OOP plugin not working

时间:2016-03-31 作者:Rodrigo D\'Agostino

我正在开发一个基于OOP的自定义插件。插件显示在插件页面中,可以在不显示任何错误的情况下激活它。但里面的功能似乎都不起作用。奇怪的是,如果我删除类方法标记,一切都会正常工作。

代码如下:

// Make sure we don\'t expose any info if called directly
if ( !function_exists( \'add_action\' ) ) {
    echo \'Hi there!  I\\\'m just a plugin, not much I can do when called directly.\';
    exit;
}

/**
 * Adds My_Custom_Plugin plugin.
 */
if ( ! class_exists( \'My_Custom_Plugin\' ) ) {

    class My_Custom_Plugin {

        /**
         * Register plugin with WordPress.
         */
        public function __construct() {

            $this->plugin_slug  = \'My_Custom_Plugin\';
            $this->version      = \'1.0.0\';
            $this->text_domain  = \'mcp\';
            $this->plugin_dir   = basename( dirname( __FILE__ ) );

            // Localization
            load_plugin_textdomain( $this->text_domain, false, $this->plugin_dir . \'/languages\' );

            // Register the settings
            add_action(\'admin_init\', array( $this, \'mcp_register_options\' ) );

            // Provide a shortcut to the settings page in plugins page
            add_filter(\'plugin_action_links\', array( $this, \'mcp_action_links\'), 10, 2);

            // Display counters on Post Edit page
            add_action(\'admin_head-post.php\', array( $this, \'mcp_display_title_counter\' ) );
            add_action(\'admin_head-post-new.php\', array( $this, \'mcp_display_title_counter\' ) );
            add_action(\'admin_head-post.php\', array( $this, \'mcp_display_excerpt_counter\' ) );
            add_action(\'admin_head-post-new.php\', array( $this, \'mcp_display_excerpt_counter\' ) );

        }

        // Define the settings
        function mcp_register_options() {

            add_settings_field( \'mcp_title_count\', __(\'Suggested maximum amount of characters for the title\', $this->text_domain), \'mcp_title_count_callback\', \'writing\', \'default\' );
            add_settings_field( \'mcp_excerpt_count\', __(\'Suggested maximum amount of characters for the extract\', $this->text_domain), \'mcp_excerpt_count_callback\', \'writing\', \'default\' );

            register_setting( \'writing\', \'mcp_options\', \'mcp_sanitize_callback\' );
            register_setting( \'writing\', \'mcp_title_count\' );
            register_setting( \'writing\', \'mcp_excerpt_count\' );

        }

        // Display and fill the form field
        function mcp_title_count_callback() {
            $mcp_title_count = esc_attr( get_option( \'mcp_title_count\' ) );
            echo \'<label for="mcp_title_count">
                                <input id="mcp_title_count" name="mcp_title_count" type="number" value="\'. $mcp_title_count .\'"/>
                            </label>\';
        }

        function mcp_excerpt_count_callback() {

        $mcp_excerpt_count = esc_attr( get_option( \'mcp_excerpt_count\' ) );
        echo \'<label for="mcp_excerpt_count">
                            <input id="mcp_excerpt_count" name="mcp_excerpt_count" type="number" value="\'. $mcp_excerpt_count .\'"/>
                        </label>\';

        }

        // Provide a shortcut to the settings page in plugins page
        function mcp_action_links($links, $file) {

            static $mcp;

            if (!$mcp) { $mcp = plugin_basename(__FILE__); }

            if ($file == $mcp) {
                // The "page" query string value must be equal to the slug
                // of the Settings admin page we defined earlier, which in
                // this case equals "myplugin-settings".
                $settings_link = \'<a href="\' . get_bloginfo(\'wpurl\') . \'/wp-admin/options-writing.php">\'. __(\'Settings\', $this->text_domain) .\'</a>\';
                array_unshift($links, $settings_link);
            }

            return $links;
        }

        // Display the Title Character Counter in the post editor
        function mcp_display_title_counter() { ?>
        <script>
                jQuery(document).ready(function() {
            jQuery("#titlediv #titlewrap").before("<div style=\\"line-height:24px;margin-bottom:5px;padding:0 10px;color:#666;\\"><strong>Caracteres:</strong><input type=\\"text\\" value=\\"0\\" maxlength=\\"3\\" size=\\"3\\" id=\\"title_counter\\" readonly=\\"\\" style=\\"width:27px;background:none;border:none;box-shadow:none;text-align:left;\\"><span>(sugeridos: <?php get_option(\'mcp_title_count\'); ?>)</span></div>");
            jQuery("#title_counter").val(jQuery("#title").val().length);
            jQuery("#title").keyup( function() {
              jQuery("#title_counter").val(jQuery("#title").val().length);
            });
            });
        </script>\';
        <?php }

        // Display the Excerpt Character Counter in the post editor
        function mcp_display_excerpt_counter() { ?>
        <script>
                jQuery(document).ready(function() {
              jQuery("#postexcerpt #excerpt").after("<div style=\\" \\"><strong>Caracteres:</strong><input type=\\"text\\" value=\\"0\\" maxlength=\\"3\\" size=\\"3\\" id=\\"excerpt_counter\\" readonly=\\"\\" style=\\"width:35px;background:none;border:none;box-shadow:none;text-align:left;\\"><span>(sugeridos: <?php get_option(\'mcp_excerpt_count\'); ?>)</span></div>");
              jQuery("#excerpt_counter").val(jQuery("#excerpt").val().length);
              jQuery("#excerpt").keyup( function() {
                jQuery("#excerpt_counter").val(jQuery("#excerpt").val().length);
              });
            });
        </script>
        <?php }

    } // class My_Custom_Plugin

}

1 个回复
最合适的回答,由SO网友:Rarst 整理而成

如果这是插件的全部,那么您永远不会创建类的对象实例。类本身只是一个定义。就像函数除非被调用否则不会做任何事情一样,定义也不会做任何事情,除非它的对象被创建。

因此new My_Custom_Plugin();.

请注意,如果您想成为一个好公民,您应该允许其他人以某种方式访问该实例,如果他们想出于某种目的与您的代码交互。不幸的是,WP没有为其建立机制,典型的低工作量解决方案是简单地将实例存储在一个全局变量中(具有合理的唯一名称)。

相关推荐

Vimeo froogaloop

我是个新手,但我想用this idea 在Wordpress站点上嵌入的Vimeo剪辑中禁用正向搜索我的职能。我添加了phpfunction frogaloop_scripts() { wp_register_script(\'snippet\', \'https://siteurl/wp-content/themes/themename/js/snippet.js\'); wp_register_script(\'frogaloop\',\'https://f.vimeo