workpress change page title from shortcode

Solutions on MaxInterview for workpress change page title from shortcode by the best coders in the world

showing results for - "workpress change page title from shortcode"
Ben
23 May 2020
1add_filter( 'pre_get_document_title', function( $title ) {
2    global $post;
3    if ( ! $post || ! $post->post_content ) {
4        return $title;
5    }
6    if ( preg_match( '#\[mc_set_title.*\]#', $post->post_content, $matches ) !== 1 ) {
7        return '';
8    }
9    return do_shortcode( $matches[0] );
10} );
11
12add_shortcode( 'mc_set_title', function( $atts ) {
13    if ( ! doing_filter( 'pre_get_document_title' ) ) {
14        # just remove the shortcode from post content in normal shortcode processing
15        return '';
16    }
17    # in filter 'pre_get_document_title' - you can use $atts and global $post to compute the title
18    return 'MC TITLE';
19} );