• joshindo

    (@joshindo)


    I have a table named ‘wp_myadmin’ with the structure below (there is only one row of data):

    | id | president | vice |
    | — | — | — |
    | 1 | John | Doe |

    I want to create a shortcode with an attribute that will show data according to the column name.

    For example:

    • [myadmin as=”president”] will show “John”
    • [myadmin as=”vice”] will show “Doe”

    Here is my current code (still a regular shortcode):

    
    function myshortcode_myadmin() {
        $name_president = "John";
        return $name_president;
    } add_shortcode('myadmin', 'myshortcode_myadmin');
    
    • [myadmin] is showing “John” (static)
      [myadmin as=”president] and [myadmin as=”vice”] also are showing “John”, because there is no attribute setting yet.

    What I want is:

    • [myadmin as=”president”] will show “John”.
    • [myadmin as=”vice”] will show “Doe”.

    Roughly the SQL query is:
    SELECT %s FROM wp_myadmin

    Can anybody help me or give me some reference please? Thanks a bunch!

Viewing 1 replies (of 1 total)
  • elangovan

    (@elangovan)

    Hi,

    Please check the below code. Its might be helpful to you.

    function myshortcode_myadmin($attr){
     
        $args = shortcode_atts( array(
         
                'as' => 'vice'
     
            ), $attr );
    global $wpdb;
    $as_args = $args['as']	
    $admin = $wpdb->get_var( "SELECT $as_args as name FROM $wpdb->myadmin" );
    		
     
        $output = $admin->name;
        return $output;
     
    }
     
    add_shortcode( 'myadmin' , 'myshortcode_myadmin' );

    [myadmin as=’vice’] will show Doe
    [myadmin as=’president’] will show John

    But if you have multiple records in this table you have to pass one more argument like admin ID and add where condition in the query.

    Thanks

    • This reply was modified 5 years ago by elangovan.
    • This reply was modified 5 years ago by bcworkz.
Viewing 1 replies (of 1 total)

The topic ‘Create Shortcode With Custom Attributes That Show DB Table Column Value’ is closed to new replies.