Answers for "workpress change page title from shortcode"

PHP
0

workpress change page title from shortcode

add_filter( 'pre_get_document_title', function( $title ) {
    global $post;
    if ( ! $post || ! $post->post_content ) {
        return $title;
    }
    if ( preg_match( '#\[mc_set_title.*\]#', $post->post_content, $matches ) !== 1 ) {
        return '';
    }
    return do_shortcode( $matches[0] );
} );

add_shortcode( 'mc_set_title', function( $atts ) {
    if ( ! doing_filter( 'pre_get_document_title' ) ) {
        # just remove the shortcode from post content in normal shortcode processing
        return '';
    }
    # in filter 'pre_get_document_title' - you can use $atts and global $post to compute the title
    return 'MC TITLE';
} );
Posted by: Guest on July-20-2020

Browse Popular Code Answers by Language