Ads

We help in wordpress site design and developmet, contact us by completing the form.

How To Add ShortCodes (PHP) In WordPress Editor

Taimoor Sattar Posted on

WordPress Shortcode as the name suggests it is used to create codes or function that can be reusable in WordPress post, page and custom post type without writing it again and again. We can embed in our post and pages in the editor of the wp-admin area and change its parameter.

For example, we can display the galleries image in medium size the Shortcode for can be written as:
[galleries size="medium"]
To define this above shortcode, create a shortcode in the function.php or plugin file named 'galleries' by using add_shortcode('galleries','function_call') pre-define function in WordPress. We can change the value of size as small, medium or large to change the size of the galleries image. Here 'function_call' parameter in the add_shortcode function is responsible for the behavior of galleries shortcode. Let look at some other example

Example 1: Writing Shortcode

You can write below PHP code in wordpress function.php or plugin PHP file.

function loremipsum($params= array(), $content = null) {
return 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas non enim sed leo facilisis eleifend eu vel lorem.';
}
add_shortcode('display-content','loremipsum');

[display-content]
To check this shortcode working, Write the following below line in the editor of blog or page; the returning value of the function will be displayed on the published page.

Example 2: Writing Shortcode

You can write below PHP code in wordpress function.php or plugin PHP file.

function highlight($params= array(), $content = null) {
return '' . $content . '';
}
add_shortcode('bg-yellow','highlight');

[bg-yellow]Lorem ipsum color me yellow.[/bg-yellow]
To check this shortcode working, Write the following below line in the editor of blog or page; the returning value of the function will be displayed in the publish page. In the publish page, the background color of this text will be yellow.

Example 3: Writing Shortcode

You can write below PHP code in wordpress function.php or plugin PHP file.

function paint($params, $content = null) {
extract(shortcode_atts(array(
'color' => ''
), $params));
return
'<span style="background:'. $color .'">' . $content . '</span>';
}
add_shortcode('bg-change','yellow');

[bg-change color="red"]Color me as you want.[/bg-change]
Write the above code in the editor of post or pages, and text background will be change in the publish page.

Ads

We help in wordpress site design and developmet, contact us by completing the form.

From Our blogs

Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *


Recent Comments
    No comments found.