2016-02-04 45 views
0

我試圖從wordpress網站上的一個頁面中刪除statcounter腳本(我已經在頁腳中),因此它不會計入該頁面,具體是因爲它是隱藏的元素。只從一個頁面中刪除statcounter腳本

我已經看過插件的PHP,但它沒有註冊或排隊任何腳本,所以我不知道使用什麼函數。下面是它的PHP片段:

$sc_position = get_option(key_sc_position); 
if ($sc_position=="header") { 
    add_action('wp_head', 'add_statcounter'); 
} else { 
    add_action('wp_footer', 'add_statcounter'); 
} 



// The guts of the StatCounter script 
function add_statcounter() { 
    global $user_level; 
    $sc_project = get_option(key_sc_project); 
    $sc_security = get_option(key_sc_security); 
    $sc_invisible = 0; 
    $sc_invisible = get_option('sc_invisible'); 
    if (
     ($sc_project > 0) 
    ) { 
?> 
    <!-- Start of StatCounter Code --> 
    <script> 
    <!-- 
     var sc_project=<?php echo $sc_project; ?>; 
     var sc_security="<?php echo $sc_security; ?>"; 
<?php 
if($sc_invisible==1) { 
    echo "  var sc_invisible=1;\n"; 
} 

define('HTTPS', isset($_SERVER['HTTPS']) && filter_var($_SERVER['HTTPS'], FILTER_VALIDATE_BOOLEAN)); 
$protocol = defined('HTTPS') ? "https:" : "http:"; 

?> 
     var scJsHost = (("https:" == document.location.protocol) ? 
     "https://secure." : "http://www."); 
    //--> 
document.write("<sc"+"ript src='" +scJsHost +"statcounter.com/counter/counter.js'></"+"script>"); 
</script> 

    <!-- End of StatCounter Code --> 
<?php 
    } 
} 

我已經嘗試使用:

function no_stat_counter_scripts() { 
    if(!is_page(XXX)){ 
     global $user_level; 
remove_action('wp_head', array($user_level, 'add_statcounter')); 
remove_action('wp_footer', array($user_level, 'add_statcounter')); 
} 
} 
add_action('wp_footer', 'no_stat_counter_scripts'); 
add_action('wp_head', 'no_stat_counter_scripts'); 

,但它不工作,任何人有一個想法,我怎麼能做到呢?

回答

0

讓你看到這一點:

$sc_position = get_option(key_sc_position); 
if ($sc_position=="header") { 
    add_action('wp_head', 'add_statcounter'); 
} else { 
    add_action('wp_footer', 'add_statcounter'); 
} 

我們不想這樣做,如果它是一個特定的網頁...

爲了得到某些頁面看看這個

How to get the current page name in WordPress?

if ($pagename != "thepageyoudontwanttohavestatson") 
{ 
if ($sc_position=="header") { 
    add_action('wp_head', 'add_statcounter'); 
} else { 
    add_action('wp_footer', 'add_statcounter'); 
} 
} 

這意味着,如果我當前的頁面名稱不等於頁面我想忽略,然後添加計數器...並擺脫代碼你做的儘可能

add_action('wp_footer', 'no_stat_counter_scripts'); 
add_action('wp_head', 'no_stat_counter_scripts'); 
0

嘗試:

<?php 
function add_statcounter() { 
if(is_page('id or slug of page to exclude')) return; 
    global $user_level; 
    $sc_project = get_option(key_sc_project); 
    $sc_security = get_option(key_sc_security); 
    $sc_invisible = 0; 
    $sc_invisible = get_option('sc_invisible'); 
    if (($sc_project > 0)) { 
    ?> 
    <!-- Start of StatCounter Code --> 
    <script> 
    <!-- 
    var sc_project=<?php echo $sc_project; ?>; 
    var sc_security="<?php echo $sc_security; ?>"; 
    <?php 
    if($sc_invisible==1) { 
     echo "var sc_invisible=1;\n"; 
    } 

    define('HTTPS', isset($_SERVER['HTTPS']) && filter_var($_SERVER['HTTPS'], FILTER_VALIDATE_BOOLEAN)); 
    $protocol = defined('HTTPS') ? "https:" : "http:"; 

    ?> 
    var scJsHost = (("https:" == document.location.protocol) ? 
    "https://secure." : "http://www."); 
    //--> 
    document.write("<sc"+"ript src='" +scJsHost +"statcounter.com/counter/counter.js'></"+"script>"); 
    </script> 
    <!-- End of StatCounter Code --> 
    <?php 
    } 
}