Remove option to allow trackbacks/pingbacks from post page options

I'm trying to disable all functionality related to pingbacks/trackbacks in WordPress and so far I have this:

add_action( 'pre_ping', 'disable_pingback' );

function disable_pingback( $links ) {
    foreach ( $links as $l = $link ) {
        if ( 0 === strpos( $link, get_option( 'home' ) ) ) {
            unset( $links[ $l ] );
        }
    }
}

However when I open up the page options and enable discussion, I still see this:

I found this answer (method 2), but it is over 5 years old now and I wasn't sure if totally replacing the whole section was the best way to do things compatibility wise, so I am asking again...

Is there a cleaner way to accomplish this?

Topic php pingbacks posts customization Wordpress

Category Web


The WordPress Codex solutions weren't working for me regardless of what parameters I used or the priorities set, probably because my setup is pretty complex or as others have commented. The first chunk of code may work for you but I don't have a default environment at the moment to test. The second chunk of code definitely worked for me. The last chunk of code removes the option from the post's quick edit as well.

You will still need to disable the functionality as you have done.

Wordpress Codex solution, (hooks into add_meta_boxes so it fires last, but you can use admin_menu or do_meta_boxes instead). Documentation Here

function remove_trackbacks_pingbacks() {

            remove_meta_box('trackbacksdiv', 'post', 'normal');

}
add_action('add_meta_boxes', 'remove_trackbacks_pingbacks');

This may be overkill, but this is what actually worked for me. Note that it will remove the option in the box from displaying everywhere, including pages too. It also allows you to customize the HTML as well just in case you wanted to add a note to tell the user that Pingbacks and Trackbacks have been disabled. (this may or may not suit your needs, but here it is anyway). Documentation Here

    function remove_trackbacks_pingbacks ($post_type, $post) {

    global $wp_meta_boxes, $current_screen;

    # Remove "ping_status" from `commentstatusdiv`:
    $wp_meta_boxes[$current_screen->id]['normal']['core']['commentstatusdiv']['callback'] = function($post) {

        ?>
            <input name="advanced_view" type="hidden" value="1">
            <p class="meta-options">
                <label for="comment_status" class="selectit">
                    <input id="comment_status" name="comment_status" type="checkbox" value="open" <?php checked($post->comment_status, 'open'); ?>> Allow comments?
                </label>
                <?php do_action('post_comment_status_meta_box-options', $post); ?>
            </p>
        <?php

    };

}

add_action('add_meta_boxes', 'remove_trackbacks_pingbacks', 10, 2);

Lastly, since you want to remove the option from your edit screen, I assume you may want to remove from the quick edit screen as well. There are no hooks or classes to hook into that I know of, so a pure jQuery solution is in order. Insert into functions.php

Modified from this solution Here

    add_action( 'admin_head-edit.php', 'remove_pings_quickedit' );

function remove_pings_quickedit() 
{    

    global $current_screen;
     if( 'edit-post' != $current_screen->id )
        return;
    ?>
    <script type="text/javascript">         
        jQuery(document).ready( function($) {           
            $('span:contains("Allow Pings")').each(function (i) {
                $(this).remove();
            });
            $('input[name=ping_status]').each(function (i) {
                $(this).remove();
            });
                   });    
    </script>
    <?php
}

The way described in the answer you point to is the easier to understand way using PHP. I agree that from a code stability POV it sucks as you need to make sure that nothing else ever changes in that meta box.

The other way to do it in php is to remove the core metabox, and create one of your own, in which you call the original one aand filter out the relevant output. It is still ugly, and you will have to write a long comment for it explaining the WHY of it, but it is more likely to remain stable than the first way.

Using JS or CSS to hide that checkbox is likely to be a much more stable idea. You are still betting against the ID being changed, but this is probably much less likely to happen and produce a much easier to understand code.


Here's the WordPress recommended way of doing it:

function remove_post_custom_fields() {
    // Remove Discussion meta box
    remove_meta_box('commentstatusdiv', 'post', 'normal');

    // Remove Comments meta box
    remove_meta_box('commentsdiv', 'post', 'normal');
}
add_action('admin_menu', 'remove_post_custom_fields');

Replace post with page or with another custom post type if needed.

Relevant codex link:
https://codex.wordpress.org/Function_Reference/remove_meta_box

Enjoy!


Please put below code on your theme's functions.php file. I hope it will work for you

function hide_ping_track() {    
    echo "<style>.meta-options label[for=ping_status], #ping_status{display:none !important;}</style>";      
}
add_action('admin_head-post.php', 'hide_ping_track');

Your code is correct, AFAIK. It will disable the ability to do trackback/pings. It doesn't remove that option from the screen, as you have noticed.

Not sure how to test that, though. But your code should work to disable the actual process of allowing trackback/ping.

This site might test trackback/pings https://tech.wizbangblog.com/ping.php . No experience with it, so YMMV.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.