Filters for Link Suggestion Metabox and SEO Metabox not working

#799454
  • Resolved Anonymous
    Rank Math free

    Dear Rank Math Support Team,

    I’m writing to you with a request for assistance regarding the use of the filters provided in the “Rank Math SEO Filters and Hooks for Developers” blog post.

    Issue with the Link Suggestion Metabox filter: I’ve tried to use the following filter to enable/disable the Link Suggestion Metabox for a specific post type, but it’s not working as expected:

    add_filter( 'rank_math/settings/titles/link_suggestions', function( $default, $post_type ) {
    return 'off';
    }, 10, 2 );

    The Link Suggestion Metabox is still being displayed when I edit or create a new post, despite using this filter.

    Issue with the SEO Metabox filter: I’ve tried to use the following filter to show/hide the SEO Metabox:

    add_filter( 'rank_math/metabox/add_seo_metabox', function( $default ) {
    return 'off';
    });

    This code also does not work, as the SEO Metabox is still being displayed when I edit or create a post. However, I found that the following code does work to remove the SEO Metabox:

    add_filter( 'rank_math/metabox/add_seo_metabox', '__return_false');

    But I’m unable to find a way to apply this filter to only specific post types.

    Attempted solution for post-type-specific SEO Metabox filtering: I’ve tried the following code to disable the SEO Metabox for a specific post type (in this case, ‘custom_templates’):

    function rank_math_setup_disable_metabox() {
    // Check if we are on the post editing screen and the post ID is set
    if (isset($_GET['post'])) {
    $post_id = $_GET['post'];
    $post = get_post($post_id);
    $post_type = get_post_type($post);

    // Checking post type here
    if ($post_type === 'custom_templates') {
    add_filter('rank_math/metabox/add_seo_metabox', '__return_false');
    }
    } elseif (isset($_GET['post_type']) && $_GET['post_type'] === 'custom_templates') {
    // This is for handling new post creation of specific post type
    add_filter('rank_math/metabox/add_seo_metabox', '__return_false');
    }
    }

    // Hook this function into admin_init
    add_action('admin_init', 'rank_math_setup_disable_metabox');

    However, the SEO Metabox is still being displayed, even for the ‘custom_templates’ post type.

    I would greatly appreciate it if you could provide some guidance on how to properly use the filters mentioned in the “Rank Math SEO Filters and Hooks for Developers” blog post to achieve the desired functionality. Any assistance you can offer would be tremendously helpful.

    Thank you for your time and attention.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Anonymous
    Rank Math free

    After a little bit of trying I’ve found solutions.

    Issue with the Link Suggestion Metabox filter
    I saw there is a setting in Rank Math to turn on and off the Link Suggestions for specific post types, this solved my issue.

    Issue with the SEO Metabox filter
    I got a working code which is now disabling the SEO Metabox from specific post types

       function disable_seo_metabox_for_templates($default) {
        // Check if we are in the admin area and a post is being edited or created
        if (is_admin()) {
            global $typenow;  // Holds the post type of the current post being edited in the admin dashboard
    
            // For cases where 'post' parameter is present, typically on the edit post screen
            if (isset($_GET['post'])) {
                $post_id = $_GET['post'];
                $post = get_post($post_id);
                $post_type = get_post_type($post);
            } elseif (isset($_GET['post_type'])) {
                // This is typically when adding a new post
                $post_type = $_GET['post_type'];
            } else {
                // Use the global typenow if other checks didn't work (useful when editing a post)
                $post_type = $typenow;
            }
    
            // Disable the MetaBox if the post type is 'custom_templates'
            if ($post_type === 'custom_templates') {
                return false;
            }
        }
    
        // Return the default otherwise
        return $default;
    }
    
    add_filter('rank_math/metabox/add_seo_metabox', 'disable_seo_metabox_for_templates', 10, 1)

    However, I noticed as well in the Rank Math settings that there is an option to turn off the SEO Metabox for specific post types.

    Turn off link counter for custom post types
    Another thing I wanted to turn off was the link counter (rank_math_internal_links_processed in the database) for the custom post types and this code works:

    function disable_post_processing_for_templates($value, $post) {
        // Check if the post object is valid and determine the post type
        if ($post && get_class($post) === 'WP_Post') {
            $post_type = get_post_type($post);
    
            // Disable processing if the post type is 'custom_templates'
            if ($post_type === 'custom_templates') {
                return false;
            }
        }
    
        // Return the original value otherwise
        return $value;
    }
    
    add_filter('rank_math/links/process_post', 'disable_post_processing_for_templates', 10, 2);

    I hope this will help anyone else with the same questions.

    Hello,

    We are glad to hear that this issue has been resolved. Thank you for letting us know. This ticket will be closed now, but you can always open a new one if you have any other questions or concerns. We are here to help you with anything related to Rank Math.

    We appreciate your patience and cooperation throughout this process.

    Thank you for choosing Rank Math.

Viewing 2 replies - 1 through 2 (of 2 total)

The ticket ‘Filters for Link Suggestion Metabox and SEO Metabox not working’ is closed to new replies.