0

I have a very simple function to create a shortcode to get the name of my page. I was using this shortcode in a form, but WordPress changed the Shortcode API and that no longer works. So I was wondering if I could use a second function to make a shortcode that would put in the HTML plus the result of the first shortcode?

This is what I have at the moment...

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

and then in the page I have this html...

<input type=hidden name=itemname value='[page_title] small print'>    

Can I make a new function that will have the input field plus the page title as a shortcode so I can replace the whole input field with a shortcode? Apologies is this sounds odd, I'm a total newbie at this!

2 Answers 2

0

Maybe you're looking for do_shortcode(), which simply executes a shortcode?

Reference: https://developer.wordpress.org/reference/functions/do_shortcode/

Still, executing a shortcode within a shortcode sounds weird. You should better make your old (first) shortcode a PHP function and then just call it from each shortcode.

1
  • sorry i don't think my code showed up properly the first time. I have a function that gets the page title and turns it into a shortcode but I can no longer use it in the input field in my form. I'm trying to find another way to do the same thing. If that makes sense?
    – Den
    Commented Jul 31, 2015 at 9:49
0

You can handle this by other way.

<?php

function my_title( ){
   $title =  get_the_title(); 
   return 'page_title="' . $title . '"';
}
add_shortcode( 'page_title', 'my_title' );

function input_shortcode($atts = null ) {    
     extract( shortcode_atts( array('page_title' => 'page_title'), $atts ) );
     return "<input type=hidden name=itemname value='" . $page_title . " small print'>"
}

add_shortcode('myInput', 'input_shortcode');

//Use it like:
$page_title = 
[myInput [page_title]]
?>

Note: These limitations may change in future versions of WordPress, you should test to be absolutely sure.

Reference: http://speckyboy.com/2011/07/18/getting-started-with-wordpress-shortcodes-examples/

1
  • Thanks you for your help. I gave that a try but it didn't work for me. I think I'll go back to an earlier version of wordpress until i can work it out.
    – Den
    Commented Jul 31, 2015 at 11:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.