shortcode - Can I use a function to return HTML and the result of another function?

admin2025-01-08  6

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!

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!

Share Improve this question edited Jul 31, 2015 at 9:47 Den asked Jul 31, 2015 at 8:37 DenDen 13 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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.

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/

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736266389a1116.html

最新回复(0)