php
  • wordpress
  • 2017-01-16 35 views 0 likes 
    0

    執行函數在某個網站時,下面的函數直接調用:覆寫在PHP

    <div class="entry-meta"> 
        <?php sparkling_posted_on(); ?> 
    

    功能是這樣定義:

    if (! function_exists('sparkling_posted_on')) : 
    function sparkling_posted_on() { 
    $time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time>'; 
    if (get_the_time('U') !== get_the_modified_time('U')) { 
        $time_string .= '<time class="updated" datetime="%3$s">%4$s</time>'; 
    } 
    $time_string = sprintf($time_string, 
        esc_attr(get_the_date('c')), 
        esc_html(get_the_date()), 
        esc_attr(get_the_modified_date('c')), 
        esc_html(get_the_modified_date()) 
    ); 
    printf('<span class="posted-on"><i class="fa fa-calendar"></i> %1$s</span><span class="byline"> <i class="fa fa-user"></i> %2$s</span>', 
        sprintf('<a href="%1$s" rel="bookmark">%2$s</a>', 
         esc_url(get_permalink()), 
         $time_string 
        ), 
        sprintf('<span class="author vcard"><a class="url fn n" href="%1$s">%2$s</a></span>', 
         esc_url(get_author_posts_url(get_the_author_meta('ID'))), 
         esc_html(get_the_author()) 
        ) 
    ); } endif; 
    

    如何防止功能的運行,或導致它不返回任何值,也無法更改原始代碼(即只能添加更多的外部代碼)。

    我嘗試使用以下,但它不會有任何影響:

    add_filter('sparkling_posted_on', '__return_false', 100); 
    

    注:這是正在發生一個WordPress網站上。然而,Wordpress SE建議像SO這樣的問題,這與一般的PHP實踐相關。

    我的目標是從本質上阻止此功能顯示博客帖子的元數據(作者/日期),而無法控制主題的原始代碼。

    +0

    使用兒童主題覆蓋功能? –

    +0

    @SebastianBrosch這是可能的,但問題仍然是需要將哪些代碼放在那裏。由於兒童主題的PHP會覆蓋它們,所以編輯出的代碼使用子主題的問題(根據我的理解)後續PHP文件的主題更新將不適用。 – Thredolsen

    回答

    0

    正如你看到的,被定義功能,當你有這樣的條件:

    if (! function_exists('sparkling_posted_on')) : 
    

    這基本上說,這將定義功能只有在不之前定義。

    所以,基本上,你只需要做

    function sparkling_posted_on() {} 
    

    別的地方以前在到達代碼點,你會重寫功能(和不會做的事)。

    我假設的功能是在主題的functions.php定義的,所以不碰代碼重寫你有兩種可能性:

    • 創建child theme,並覆蓋在主題功能。 (儘管如此,父主題的更新將被應用,這是兒童主題吸引力的一部分)

    • 創建plugin來定義該函數,因爲插件在主題之前加載。

    兩者都不應該花費太多的工作。插件可能(稍微)更簡單。

    如:這是一個最小表達式插件,可以覆蓋功能:

    <?php 
    /* 
    Plugin Name: Sparkling Override 
    Description: Overrides "sparkling_posted_on()", defined in Such Theme 
    Version:  XP 
    */ 
    
    if (! function_exists('sparkling_posted_on')) : 
        function sparkling_posted_on() {} 
    endif; 
    

    (這是不太一般的PHP的問題,更多的是WP開發的問題,所以我想這可能對WPSE幸福地生活儘管如此,這不是錯誤的)。

    +0

    這很好,謝謝。 – Thredolsen

    -1

    可能需要使用remove_action函數。

    <?php 
    //put this in your theme's functions.php 
    //returns @bool 
    remove_action('init', 'sparkling_posted_on'); 
    ?> 
    

    來源:https://codex.wordpress.org/Function_Reference/remove_action

    +1

    感謝您的意見。不幸的是,這似乎沒有任何效果。 – Thredolsen

    相關問題