Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

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';
} );
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #workpress #change #page #title #shortcode
ADD COMMENT
Topic
Name
5+6 =