Running PHP code inside content is not allowed instead you can use the WP Shortcode.
You can register add_shortcode()
in your function.php
the file of your theme. That is the easiest way to create a shortcode.
You can add the following code to function.php
file of your theme.
add_shortcode( 'helloworld_shortcode', 'fn_helloworld_shortcode' );
function fn_helloworld_shortcode() {
return 'Hello World';
}
This is the very basic shortcode you can create in WorldPress
add_shortcode()
function is used to register the shortcode[helloworld_shortcode]
in your editor to call the shortcode function.This is not the best practice to make the shortcode ( We put the code inside function.php
).
Creating a plugin and put this code inside it, is the best practice.
Shortcode can accept parameters that are called attributes.
This is a shortcode with content and attributes
[tc_quote author="Brian Dean"]SEO isn’t about content creation. It’s about content promotion [/tc_quote]
You can put the content inside the square bracket.
How can you access content and attributes inside the function?. It is very simple. You can use the function shortcode_atts()
Complete code to access attribute and content
add_shortcode( 'tc_quote', 'fn_tc_quote' );
function fn_tc_quote($attributes, $content = null) {
return "<div >".$content."</div><div> <b>".$attributes['author']."</b> </div>";
}
These are the two shotcodes I have developed for this site to display quotes and related posts
Quote Shortcode
Related Posts Shortcode