我正在开发一个基于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
}