vapvarun icon

Insert Blog slug for all blog post

vapvarun | PRO | 04/07/21 10:39:59 AM UTC | 0 ⭐ | 1099 👁️ | Never ⏰ | []
PHP |

830 B

|

None

|

0 👍

/

0 👎

function wb_create_new_url_querystring() {
    add_rewrite_rule(
        'blog/([^/]*)$',
        'index.php?name=$matches[1]',
        'top'
    );
 
    add_rewrite_tag('%blog%','([^/]*)');
}
add_action('init', 'wb_create_new_url_querystring', 999 );
 
 
/**
 * Modify post link
 * This will print /blog/post-name instead of /post-name
 */
function wb_append_query_string( $url, $post, $leavename ) {
 
    if ( $post->post_type != 'post' )
            return $url;
    
    
    if ( false !== strpos( $url, '%postname%' ) ) {
            $slug = '%postname%';
    }
    elseif ( $post->post_name ) {
            $slug = $post->post_name;
    }
    else {
        $slug = sanitize_title( $post->post_title );
    }
    
    $url = home_url( user_trailingslashit( 'blog/'. $slug ) );
 
    return $url;
}
add_filter( 'post_link', 'wb_append_query_string', 10, 3 );

Comments