When you want to return a data combined with a string, you can use one of the following methods:
Either open and close the string using '
:
return '<span class="class2" id="class3-' . $random . '"> </span>';
Or use the double quote:
return "<span class="class2" id='class3-{$random}'> </span>";
You can simply use $random
without the {}
curly brackets, but it’s easier to read if you use {}
.
You can even use double quotes for the inner string, but you need to escape them:
return "<span class=\"class2\" id=\"class3-{$random}\"> </span>";
As pointed out in comments by @birgire, to make it more WordPressy, we can also escape the variable:
return sprintf( '<span class="class2" id="class3-%s"></span>', esc_attr( $random ) );
Related Posts:
- Return HTML Template Page with PHP Function
- How do I use Shortcodes inside of HTML tags?
- WordPress Shortcode and Dynamic CSS
- Add/echo div with Analytics-Code to function in functions.php
- Creating a WordPress shortcode
- How do I hardcode a WordPress shortcode into my theme?
- has_shortcode() – how to detect nested shortcode
- How to return multiples lines in a shortcode?
- How do I turn a shortcode into PHP code?
- How to return a foreach inside a shortcode
- Placing raw HTML inside a WordPress shortcode
- custom shortcode will not display the wrapped content
- Shortcode content output but not in correct place
- PHP code inside shortcodes
- Trigger popup in a php if/else statement
- Append HTML Using Shortcode
- How to output a PHP file values by shortcode?
- How to display posts by current user in a drop down
- Colon is Missing In My Website Url in WordPress
- Pass php dynamic variable to shortcode
- wordpress 4.4 self hosted video (html5) doesn’t show
- do_shortcode with custom field
- Remove echo from shortcode
- Shortcode to show the code
- Get URL from shortcode tag
- Multiple do_shortcode in page template
- get_posts works in the page template but not in a shortcode
- How can I include shortcodes within PHP?
- $wpdb returns duplicate posts
- Syntax error in a shortcode function
- Shortcode to find and replace URL
- Could use some help with Shortcodes
- How to put a form with php code into a variable or shortcode?
- Help with WordPress function inside a shortcode
- Short code template + ajax
- WordPress shortcode returns the data before
- Shortcode to embed Edit Account form not working
- Handling PHP/HTML inside the output of a shortcode function
- Need to Modify a WordPress Shortcode with another wordpress shortcode
- Split shortcodes to array of shortcodes
- Shortcodes showing in excerpt despite using strip_shortcodes
- PHP error with a shortcode: “no suitable wrapper” for file_get_contents
- How to make a conditional statement within $output in shortcodes.php?
- Shortcode for a Series of Elevating Image Files, Excluding a Range?
- Shortcode won’t execute
- How to get my [shortcode] rendered on the home page, other pages are fine
- Shortcode displaying at the top of the page [duplicate]
- AJAX function not working [closed]
- Can ‘Custom Field’ data be inserted into this Short Code?
- MySQL Query Returns Array () In Shortcode
- Why wp_remote_get() fetches certain web pages while it doesn’t work for my localhost?
- How to exclude labels from certain categories in a shortcode?
- Set document title through shortcode plugin
- Trying to call dynamic content into a 3rd party shortcode
- Shortcode in Shortcode/HTML Graphics and Pictures
- Why is the current page loaded in the pop-up window and not the specified one?
- How to search order using database frontend short code WordPress
- How do I reopen the (Popup Maker) after entering the correct password for a password protected page?
- Not able to remove caption shortcode from the content
- How to add a shortcode which runs a .php file for the current post
- Block error message in foreach loop when looping through ACF field
- Get title of page containing post grid within the posts
- PHP multiple forms, same page, isset($_POST[]) not working?
- Shortcodes not processing inside post content
- Gravity Forms: How to add PHP function to confirmation conditional shortcode?
- wpdb->query returns different value to phpMyAdmin
- shortcodes, custom php and their errors
- I want to hide “sold by” on certain pages with id page 43
- Echo short code syntax
- How to add a PHP scripts into WordPress
- How to return html as a string from php for WordPress
- Changing Date Format on Custom Meta Data w/shortcode call
- Why my Ajax events are no longer detected after dynamic change of shortcode content
- Multiple Arrays for Custom Post Query using Boostrap accordions and ACF repeater fields
- Replacing entire tag with shortcode – JavaScript [duplicate]
- Get posts by id using shortcode
- Shortcode just with a php code
- How can i iterate through this shortcode array?
- Custom fields changes the formatting of metabox input
- Shortcode is not outputting all of the desired markup
- Run PHP Results inside WP Shortcode
- Contact form field in wordpress menu
- Serializing URL via wp add_shortcode to auto select from option list onload
- php string inside shortcode does not work
- Limit the number of successful logins
- Shortcodes (with a space) added to php Sample
- On selecting shortcode generate selected shortcode table inside page editor worrdpress
- Show contact 7 form in popup
- How can I put a custom field as the link of a button shortcode?
- Custom field within shortcode
- Problem using explode inside add_shortcode() callback funciton [closed]
- Php inside the shortcode is getting commented
- My title is showing after the shortcode
- Display Current Time using shortcode
- Set variable in javascript and using in my shortcode
- How to create shortcode of this PHP code
- Redirect loop upon installation of my plugin
- why css file not link?how to create permalink?
- Show Login Errors In WordPress/Elementor (Code “works”, but breaks site)
- Shortcode Displays 2 times