Creating WordPress shortcodes. Basic and OOP code example open

Creating WordPress shortcodes. Basic and OOP code example

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators

To begin, create a simple shortcode that displays the text “Hello, im shortcode text“. Place code below in your function.php file:


function basic_shortcode_function() {
    return 'Hello, im shortcode text';
}
add_shortcode('basic_shortcode', 'basic_shortcode_function');

As a result, we will get the [basic_shortcode], ready to be pasted:
on post or in php template using the function:

<?php
echo do_shortcode('[basic_shortcode]');
?>

Shortcode with with parameters

In the following example, we will create a shortcode for inserting an image with a given size.

function basic_shortcode_function2($atts) { // function of our shortcode, if there are no <strong>$atts</strong> then output an empty array

//function basic_shortcode_function2($atts = array(), $content=null) // if we want to transfer content in the body of the shortcode [shortcode]content[/shortcode]

    extract(shortcode_atts(array( //receive the data transferred to the shortcode
        'width' => 100, //set default parameters
        'height' => 100,
    ), $atts)); //pass our variable with parameters
	
    return '<img src="/image.jpg" width="' . $width . '" height="' . $height . '"/>';
}
add_shortcode('basic_shortcode2', 'basic_shortcode_function2');

Use shortcode [basic_shortcode2 width=”150″ height=”100″]. You can now pass parameters to the shortcode width=”150″ height=”100″ which will set the desired image size.

Shortcode code example on OOP

<?php

class My_Shortcodes { //create a class

    public $allCars; //declare variables for our shortcode
    public $users;

    public function register(){
        add_action('init',[$this,'register_shortcode']); //event fired after WordPress is fully loaded
    }

    public function register_shortcode(){
        add_shortcode('my_filter',[$this,'my_shortcode']); // a function that registers our shortcode my_filter
    }

    public function my_shortcode($atts = array()){ // function of our shortcode, if there are no $ atts then output an empty array
        // public function my_shortcode($atts = array(), $content=null) // if we want to transfer content in the body of the shortcode [shortcode]content[/shortcode]
        extract(shortcode_atts(array(//receive the data transferred to the shortcode
            'price' => 0, //set default parameters
            'user' => 0,
        ),$atts));//pass our variable with parameters


        $this->allCars = new allCars();

        $this->users = get_posts(array('post_type'=>'user','numberposts'=>-1)); //get all posts of type user

        $users_list = ''; // transform ours $this->users to options
        foreach($this->users as $user){
            $users_list .= '<option value="'.$user->ID.'">'.$user->post_title.'</option>';
        }

        $output = ''; // create a variable that will output the code
        $output .= '<div class="wrapper">'; // add basic html code to our variable, open our div wrapper
        $output .= '<form method="post" action="'. get_post_type_archive_link('cars') .'">'; // create a form action to cars archive page
        
       

        if($price == 1){ // If a variable $price (is passed in the shortcode) = 1
            $output .='<input type="text" placeholder="Price" name="cars_price" value="" />';  // фвdd code to our output function
        }

        
        if($user == 1){
            $output .= '
            <select name="users">
                <option value="">Select user</option>
                '.$users_list.'
            </select>
            ';
        }
        
        $output .= '<input type="submit" name="submit" value="Submit" />';

        $output .= '</form></div>';



        return $output; //output the code

        
    }

}
$My_Shortcodes = new My_Shortcodes(); //create instance
$My_Shortcodes->register(); //call the method register
0

More

Leave a Reply

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

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation