Search code examples
wordpressshortcodecustom-fields

Custom Page Title Shortcode within another Shortcode


I use this function to obtain the title of post page in the content area:

function myshortcode_title( ){
return get_the_title();
}
add_shortcode( 'page_title', 'myshortcode_title' );

So, I have a plugin that when I use this shortcode below it read the text beetween the shortcode:

[responsivevoice voice="UK English Male"] Wonderful World [/responsivevoice]

So, what I'm doing and it's my question also is how can I put the "page title shortcode" within responsivevoice shortcode like this?

[responsivevoice voice="UK English Male"] [page_title] [/responsivevoice] 

(It should get the title of the post page, but it doesn't work).


Solution

  • Choose one of the following options, and add the code snippet after:

    add_shortcode( 'page_title', 'myshortcode_title' );
    

    Option #1: [responsivevoice2]

    add_shortcode( 'responsivevoice2', 'responsivevoice2' );
    function responsivevoice2( $atts = array(), $content = '' ) {
        if ( $content = do_shortcode( $content ) ) {
            return RV_add_bblisten( $atts, $content );
        }
        return '';
    }
    

    Sample usage:

    [responsivevoice2 voice="UK English Male"] [page_title] [/responsivevoice2]
    

    This Shortcode allows you to use the [page_title] or any other Shortcodes in the text to be spoken.

    Option #2: [responsivevoice_page_title]

    This Shortcode will always use the page title (or the title of the current post) as the text to be spoken. With this Shortcode, you don't need the [page_title] Shortcode anymore.

    add_shortcode( 'responsivevoice_page_title', 'responsivevoice_page_title' );
    function responsivevoice_page_title( $atts = array() ) {
        if ( $page_title = get_the_title() ) {
            return RV_add_bblisten( $atts, $page_title );
        }
        return '';
    }
    

    Sample usage:

    [responsivevoice_page_title voice="UK English Male" /]