无法应用选择性加载

时间:2013-09-13 作者:gurung

我想去掉标题中不必要的调用。我想一个插件“wizylike”只加载在“posts”上,而不加载其他插件(归档、页面等)。我从this thread on how to load plugin selectively 并编写了一个用于函数的小代码。php。

function remove_wizy() {
        remove_action(\'init\', \'wizylike_init\');
        remove_action(\'wp_head\', \'wizylike_head\');
        remove_action(\'wp_print_scripts\', \'wizylike\');
        remove_action(\'wp_print_styles\', \'wizylike\');
}

if( !is_single() ) {
        add_action(\'wp_head\', \'remove_wizy\');
    }
Problem : 插件仍在打印标题中所有页面的脚本。要么我无法识别正确的句柄,要么执行错误。我尝试了很多变化,但都失败了,所以我想现在我会在这里提问。下面是插件加载程序的代码。php。

// constants paths
define(\'WL_PATH\', dirname(__FILE__));

// constants URIs
define(\'WL_URI\', get_bloginfo(\'wpurl\') . \'/wp-content/plugins/wizylike\');
define(\'WL_CSSURI\', WL_URI . \'/css\');
define(\'WL_JSURI\', WL_URI . \'/js\');
define(\'WL_IMGURI\', WL_URI . \'/images\');


// Calls database global
global $wpdb, $wl_tablename;


// Combines default db tables prefix with our newly tabel name
$wl_tablename = $wpdb->prefix . \'wizylike\';


// includes plugin files
require_once(WL_PATH . \'/widget.wizylike.php\');         // Sidebar widget


// Runs when the plugin is activated
function wizylike_activate() {
    global $wpdb, $wl_tablename;

    if (!empty($wpdb->charset))
        $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";

        // run the SQL statement on the database
        $wpdb->query("CREATE TABLE {$wl_tablename} (
                            id BIGINT(20) NOT NULL AUTO_INCREMENT,
                            post_id BIGINT(20) NOT NULL,
                            ip_address VARCHAR(25) NOT NULL,
                            user_id BIGINT(20) NOT NULL,
                            like_status VARCHAR(25) NOT NULL DEFAULT \'like\',
                            PRIMARY KEY (id), 
                            UNIQUE (id)
                            ){$charset_collate};");

        $wpdb->query("ALTER TABLE `$wpdb->posts` ADD `like_count` BIGINT( 20 ) NOT NULL DEFAULT  \'0\'");

    update_option(\'wizylike_capabilities\', \'users-only\');
    update_option(\'wizylike_colour\', \'red\');
    update_option(\'wizylike_style\', \'style_1\');
    update_option(\'wizylike_like_txt\', \'Like?\');
    update_option(\'wizylike_unlike_txt\', \'Unlike!\');
    update_option(\'wizylike_widget_txt\', \'Likes\');


}
register_activation_hook(__FILE__, \'wizylike_activate\');


// Runs when the plugin is deactivated
function wizylike_deactivate() {
    global $wpdb, $wl_tablename;

    $wpdb->query("ALTER TABLE `$wpdb->posts` DROP `like_count`;");
    $wpdb->query("DROP TABLE IF EXISTS {$wl_tablename};");
}
register_deactivation_hook(__FILE__, \'wizylike_deactivate\');


// register functions
add_action(\'admin_menu\', \'wizylike_settingspage_create\');
add_action(\'init\', \'wizylike_init\');
add_action(\'admin_init\', \'wizylike_admin_init\');


// settings page create
function wizylike_settingspage_create(){

    if (isset($_POST[\'wizylike_save\'])) {
        // $wizylike_settings[\'wizylike_capabilities\'] = $_POST[\'wizylike_capabilities\'];
        $wizylike_settings[\'wizylike_unlike_txt\'] = $_POST[\'wizylike_unlike_txt\'];
        $wizylike_settings[\'wizylike_like_txt\'] = $_POST[\'wizylike_like_txt\'];
        $wizylike_settings[\'wizylike_colour\'] = $_POST[\'wizylike_colour\'];
        $wizylike_settings[\'wizylike_style\'] = $_POST[\'wizylike_style\'];
        $wizylike_settings[\'wizylike_widget_txt\'] = $_POST[\'wizylike_widget_txt\'];

        foreach($wizylike_settings as $name => $val) {
            if(isset($_POST[$name])) {
                update_option($name, $val);
            }
        }

        if(isset($_POST[\'wizylike_capabilities\'])) {
            update_option(\'wizylike_capabilities\', \'users-only\');
        } else {
            update_option(\'wizylike_capabilities\', \'all\');
        }

        header("Location: options-general.php?page=wizylike&saved=true");
        die;
    }

    $page = add_options_page( __(\'WizyLike\', \'wizylike\'), __(\'WizyLike\', \'wizylike\'), \'manage_options\', \'wizylike\', \'wizylike_settingspage\');
    add_action(\'admin_head-\' . $page, \'wizylike_adminhead\');    
}


// wizylike front-end init

function wizylike_init(){

    // includes main class
    require_once(WL_PATH . \'/class.wizylike.php\');

    // includes template tags for ease of usage
    require_once(WL_PATH . \'/template-tags.php\');

    // adds necessary stylesheets to wp_head
    wp_enqueue_style(\'wizylike\', WL_CSSURI . \'/wizylike.css\', false, \'1.0\', \'screen\');

    // adds necessary javascripts to wp_head
    wp_enqueue_script(\'jquery\');
    wp_enqueue_script(\'wizylike\', WL_JSURI . \'/wizylike.js\', false, \'1.0\', false);
}


// wizylike back-end init
function wizylike_admin_init(){
    // adds necessary stylesheets to wp_head
    wp_enqueue_style(\'wizylike-admin\', WL_CSSURI . \'/admin.css\', false, \'1.0\', \'screen\');

    // adds necessary javascripts to wp_head
    wp_enqueue_script(\'wizylike-admin\', WL_JSURI . \'/admin.js\', false, \'1.0\', false);
}


// wizylike back-end head
function wizylike_adminhead(){

}


// wizylike front-end head
function wizylike_head(){
    $js = \'<script type="text/javascript"> var wizylike_url = "\' . WL_URI . \'"; </script>\' . "\\n";
    echo apply_filters(\'wizylike_head\', $js);

    do_action(\'wizylike_head\');
}

add_action(\'wp_head\', \'wizylike_head\');

// wizylike settings page callback
function wizylike_settingspage(){
    require_once(WL_PATH . \'/wrap.php\');
}

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

试试这个。它将中设置的样式和脚本出列\'init\'.

add_action( \'template_redirect\', \'remove_wizzylike\' );

function remove_wizzylike() {
    if ( ! is_single() )
        add_action( \'wp_head\', \'remove_wizy\', 5 );
}

function remove_wizy() {
    remove_action( \'wp_head\', \'wizylike_head\' );

    // Enqueued in wizylike_init()
    wp_dequeue_style( \'wizylike\' );
    wp_dequeue_script( \'wizylike\' );
}

结束

相关推荐