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
- Is there a way to parse shortcodes in PHP?
- Display sorting options dropdown when using WooCommerce product category shortcode
- Retrieve multiple values passed to a single attribute in a shortcode
- ob_get_clean returns empty string, ob_get_flush outputs string
- Export User Info to CSV from Front End
- Counting number of posts in a category and its sub categories and displaying result using shortcode
- Shortcode created to check language not works
- Using Echo in ShortCode – Stuck
- Add/echo div with Analytics-Code to function in functions.php
- Multiple do_shortcode($content) within one shortcode
- My shortcode is showing up twice
- How can I default to all WordPress roles when parameter is not included in shortcode?
- WordPress menu deletes when trying to add a hook
- PHP contact form returns warning in text widget [closed]
- How to find the output of contact form 7 shortcode? [closed]
- How do i get (unique) page name?
- Listing and displaying WooCommerce Shipping Zones in the frontend? [closed]
- Can A Shortcode Get HTML/Text From Content And Return It Twice With Added HTML?
- Recent post display using shortcode
- Browser title script ignoring is_page /else conditional [closed]
- Need to turn this php into WP shortcode
- Making Quote Plugin more efficient
- How to limit the content coming from wordpress shortcodes?
- custom shortcode will not display the wrapped content
- Create shortcodes within foreach loop (using array)
- Trigger popup in a php if/else statement
- Append HTML Using Shortcode
- How to output a PHP file values by shortcode?
- How to display data from custom table in wordpress phpmyadmin
- Creating own shortcode – echoeing php variable based on JS variable
- How create a Shortcode with hover and complex options
- Remove from shortcode
- Shortcode returns values in the wrong order
- Embedding PHP in shortcode $content
- Shortcode with PHP issue “Undefined index”
- get_posts works in the page template but not in a shortcode
- ::before on open/close function [closed]
- Is There A Way To Make Theme Files Accept Shortcodes?
- How can I include shortcodes within PHP?
- Is it possible to define variables in a wordpress shortcode, and then call the shortcode using a specific variable?
- Create a WordPress shortcode using PHP [duplicate]
- Syntax error in a shortcode function
- Unable to process php via shortcode
- Help with WordPress function inside a shortcode
- calling a custom field value inside functions.php
- Using ob_start and ob_get_clean with wordpress shortcode
- Short code template + ajax
- WordPress shortcode returns the data before
- Cant display an image via PHP in wordpress
- help with custom shortcode that stopped working
- Shortcode for a Series of Elevating Image Files, Excluding a Range?
- Shortcode won’t execute
- AJAX function not working [closed]
- Can ‘Custom Field’ data be inserted into this Short Code?
- how to get sentence values from wordpress shortcode parameter?
- Set document title through shortcode plugin
- 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
- Shortcodes not processing inside post content
- Gravity Forms: How to add PHP function to confirmation conditional shortcode?
- wpdb->query returns different value to phpMyAdmin
- Time Stamp In A WordPress Post Title That Does Not Keep Refreshing
- How can I edit the final HTML structure of the whole web page
- I am having an issue with this shortcode plugin.. Warning: Illegal string offset ‘title’
- I want to hide “sold by” on certain pages with id page 43
- How to add a PHP scripts into WordPress
- How can I create a shortcode from an html and php code written together
- Multiple Arrays for Custom Post Query using Boostrap accordions and ACF repeater fields
- Replacing entire tag with shortcode – JavaScript [duplicate]
- CPT in a shortcode
- PHP/CSS: Shortcode won’t display correctly, and only displays in the head (before content)
- Shortcode just with a php code
- WordPress Shortcode based on other shortcode
- Force ‘permanent’ post cache of shortcode results
- Wrapping shortcode content in a span or link
- Custom fields changes the formatting of metabox input
- Use html tags in shortcode_atts, is it real?
- Contact form field in wordpress menu
- php string inside shortcode does not work
- Limit the number of successful logins
- Shortcodes (with a space) added to php Sample
- How to have Function of a plugin using global vars into a shortcode?
- How can I put a custom field as the link of a button shortcode?
- Where to place PHP for shortcodes
- How to wrap a text with shortcode into php file?
- PHP Widget and do_shortcode
- Problem using explode inside add_shortcode() callback funciton [closed]
- Shortcode not passing variable to included file
- Display Current Time using shortcode
- How to create shortcode of this PHP code
- WP Custom tables query
- Display terms on product page with shotrcode
- Custom plugin with shortcode not working
- How to edit a widget code?
- Calling a function via a shortcode in javascript
- Woocomerce custom add to cart button edit functionality [closed]
- I have a php function with a shortcode to add share buttons, but I’m having an error!
- Nav Menu Short code Not working When non Login
- Automatically populating a date parameter within a shortcode