Search code examples
phpwordpressshortcodewordpress-shortcode

How can I register in WordPress every record from a database table as a shortcode


I need some help and I hope you can help me.

I'm a beginner WordPress developer and I'm trying to develop a shortcodes plugin, that allows me to add anytime new shortcodes in WP Admin and show them in the frontend.

I'm not using the WP post types. I have created a separate table with only 2 fields: shortcode_title and shortcode_value. I can add, edit and delete shortcodes.

The problem is where I have to register each 'shortcode_title' as a shortcode. I cannot run the add_shortcode related function in a loop and I'm stuck.

The code that adds the shortcodes into the database:

if($_REQUEST['param']=="save_shortcode"){

    $form_data = array(
    'shortcode_title' => sanitize_text_field( $_REQUEST['frm_title'] ),
    'shortcode_value' => sanitize_text_field( $_REQUEST['frm_value'] )
     );

    // save data into db table
    $wpdb->insert("ds_shortcodes", $form_data);
}

Right now I have added 1 shortcode and I have written a function to add it as shortcode in WordPress.

function ds_shortcodes_page_functions_DSexe(){
global $wpdb;
$shortcode_id = "Software";
$shortcode_details = $wpdb->get_row("SELECT shortcode_value FROM ds_shortcodes WHERE shortcode_title like '$shortcode_id'", ARRAY_A);

return $shortcode_details['shortcode_value'];
}

add_shortcode("Software","ds_shortcodes_page_functions_DSexe"); 

But I have to duplicate this function manually in PHP for each shortcode I add in admin.

I have tried this:

function shortcode_content(){

global $wpdb;
$all_shortcodes = $wpdb->get_results("SELECT shortcode_title, shortcode_value FROM ds_shortcodes");

    foreach($all_shortcodes as $each_shortcode){
        return $each_shortcode['shortcode_value'];
    }

}

function shortcode_add(){

    foreach($all_shortcodes as $each_shortcode){
       add_shortcode($each_shortcode->shortcode_title,'shortcode_content');
    }

}

I have to pull all records from the database in a loop and for each 'shortcode_value' I have to add the correspondent 'shortcode_title' as a shortcode.

If I print_r $all_shortcodes I get an array like this:

Array
(
[0] => stdClass Object
    (
        [shortcode_title] => Software
        [shortcode_value] => test.exe
    )

[1] => stdClass Object
    (
        [shortcode_title] => Version
        [shortcode_value] => 10
    )

[2] => stdClass Object
    (
        [shortcode_title] => Size
        [shortcode_value] => 412 MB
    )

[3] => stdClass Object
    (
        [shortcode_title] => DS1
        [shortcode_value] => 11111111111111
    )

[4] => stdClass Object
    (
        [shortcode_title] => DS2
        [shortcode_value] => 22222222222222
    )

[5] => stdClass Object
    (
        [shortcode_title] => DS3
        [shortcode_value] => 33333333333333
    )

)

I have also tried is: It seem to make more sense but something is still missing.

function shortcode_content($short_title, $short_value){
  global $wpdb;
  $all_shortcodes = $wpdb->get_results("SELECT shortcode_title, shortcode_value FROM ds_shortcodes", ARRAY_A);
  foreach($all_shortcodes as $each_shortcode){
    $short_title =  $each_shortcode['shortcode_title'];
    $short_value =  $each_shortcode['shortcode_value'];
    return $short_title;
    return $short_value;
  }

}


function shortcode_add(){

  foreach($short_value as $key){
    add_shortcode($short_title,array('shortcode_content'));
  }

}

The second function seems not to get the variables from the first one.

I have also put the variables from the first function in an array but still doesn't work.

function shortcode_content($short){
  global $wpdb;
  $all_shortcodes = $wpdb->get_results("SELECT shortcode_title, shortcode_value FROM ds_shortcodes", ARRAY_A);
  foreach($all_shortcodes as $each_shortcode){
    $short_title =  $each_shortcode['shortcode_title'];
    $short_value =  $each_shortcode['shortcode_value'];
    $short =  array ($short_title, $short_value);
    return $short;
  }

}

function shortcode_add(){

  foreach($short_value as $key){
    add_shortcode($short_title,array('shortcode_content'));
  }

}

I think I'm getting closer but it still doesn't work. :)

I know this should be easy but as a beginner, it seems to be quite complicated.

After 2 days of trying, I would really appreciate your help.

Thank you.

Alin


Solution

  • You will need to fetch shortcode data in shortcode_add function as well

    Here is the complete working solution..

    if(! function_exists('shortcode_content')){
    
        function shortcode_content( $atts, $content = null,$tag)    {
            global $wpdb;
    
            extract( shortcode_atts( array(
                        'shortcode_extra_data' => '', // available as $shortcode_extra_data
                    ), $atts 
                ) 
            );
    
            $shortcode_value = $wpdb->get_var("SELECT shortcode_value FROM ds_shortcodes where shortcode_title = '".$tag."'");
    
            // do something with cotent
            // add shortcode_value with content provided in shortcode
            $content = $content.$shortcode_value;
    
            $content = apply_filters('the_content', $content); // use this according to your requirment, remove if not needed
            // return processed content
            return $content;
    
        }
    }
    
    /*
    * call function on init hook
    */
    add_action( 'init', 'shortcode_add' );
    if(! function_exists('shortcode_add')){
    
        function shortcode_add(){
            global $wpdb;
    
            $all_shortcodes = $wpdb->get_results("SELECT shortcode_title, shortcode_value FROM ds_shortcodes", ARRAY_A);
            foreach($all_shortcodes as $short){
                add_shortcode($short['shortcode_title'],'shortcode_content');
            }
    
        }
    }
    

    And shortcode to call

    [short1 shortcode_extra_data="syzzzzzz"]something inside content[/short1]