0

I'm getting a weird behavior from the wp admin bar when logged in. Let me explain. My project has a custom permalink structure with a string before the post name (like custom-string/%postname%/) set in the permalink settings. This works ok for posts and CPT's, but not for pages. So, to get this working for pages, I added this code sample in my functions.php file:

add_action( 'init', 'custom_page_rules' );
add_filter( 'post_link', 'change_page_links', 1, 3 );

function custom_page_rules() {
    global $wp_rewrite;
    $wp_rewrite->page_structure = $wp_rewrite->root . 'my-string/%pagename%'; 
}

function change_page_links( $post_link, $id=0 ){

    $post = get_post($id);

    if( is_object($post) && $post->post_type == 'page'){
        return home_url('/my-string/'. $post->post_name.'/');
    }

    return $post_link;
}

This code sample works, and now my pages and all links using the_permalink() or get_permalink() are working fine with the desired string. But, when I set this code, the admin bar stopped showing for all pages (including posts and CPT's single pages), except for the home page, which doesn't have the string added to the permalink.

The website works ok, and I got no errors when set define( 'WP_DEBUG', true );. If I remove the code sample, the admin bar starts to show again.

What could be causing this behavior? Any tips on how to solve this?

Thanks!

Edit:

Since I tested in a WP fresh install and couldn't reproduce the issue, I did some more research. In the install the I'm getting this issue, I deactivated all plugins and changed the theme to the default one that comes with WordPress (Twenty Fifteen), and the issue still happens.

From what I'm seeing, looks like the issue is somehow related with something at the wp_footer() call. I tried to debug the wp_footer() using this code snippet and what I found is: at the home page, where the custom permalink isn't used and the admin bar is shown, all the euqueued scripts are hooked. But at all the other pages, only plugins and themes scripts and styles are shown. Something must be turning the admin bar off, somehow.

Edit 2:

I was able to use add_filter('show_admin_bar', '__return_true') to force the admin bar to show and load the needed assets. But the admin bar loads "empty". Also, there are admin-bar and no-customize-support classes loaded at the body_class() function, but no logged-in or customize-support classes, that loads when the home page is show. Looks like WP thinks that I'm offline when I'm on a page with that custom permalink structure.

  • I've tried your code and I can not reproduce the issue. The problem may be in another piece of code. Please, provide exact steps that we can follow to reproduce your problem any other relevant code. – cybmeta Mar 5 '15 at 18:18
  • View source & check if the admin bar markup is there (it'll be near the end of the document). – TheDeadMedic Mar 5 '15 at 18:21
  • @cybmeta, in fact, I just tried to reproduce the issue in a WP fresh install but everything is working fine. The curious thing is that in the install that I'm having issues, I disabled all the plugins and switched for the default theme, but the issue still happen. I'm trying to debug it a little more to try to find the source of this. – Diego de Oliveira Mar 5 '15 at 20:35
  • @TheDeadMedic no, the admin bar markup isn't there, it's like there's an issue close to the wp_footer() function. But the curious thing is that WP_DEBUG isn't caching any php issues. – Diego de Oliveira Mar 5 '15 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.