WordPress: How to add record id to shortcode as attribute

I’ve been searching all day looking for a method to add an attribute to a shortcode. The attribute I want to add is the ID from a custom wp_table.

What I’m trying to get for output is, example: [launch_tincan id="1"]

Read More

When receiving the information of row 1 in my custom wp_table, I’m able to populate my vars with the information.

I have a function where currently, if I hardcode my get_results from a row where id=#, my shortcode populates properly, however, I want the shortcode to contain the id.

I’ve seen tons of examples using wp_posts and such, and none work for me.

The other thing I notice is that no-one seems be generating shortcodes from a function within a function. Is this possible?

Here is an example of my code:

<?php
// WP Shortcode to display info from content in my table
function my_shortcode_functions(){
    global $wpdb;
    $getinfo = $wpdb->get_results($wpdb->prepare("SELECT id,col1,col2,col3,col4 from my_table",$id));
    foreach ($getinfo as $s ){
        $col1=$s->col1;
        $col2=$s->col2;
        $col3=$s->col3;
        $col4=$s->col4;
    }
    if ( is_user_logged_in() ) {
?>
<div>
    <form method="post">
        <input type="submit" onClick="window.open(document.getElementById('results').innerHTML);" value="Open" class='button'/>
    </form>
</div>
<div style="visibility:hidden; height:0px; width:0px;" id="results">
<?php
        echo $col1 . $col2 . $col3 . $col4;
?>
</div>
<?php
    } else {
        echo "You must be logged in to use this";
    }
}
add_shortcode('my_shortcode', 'my_shortcode_functions');
?>

Is it possible to have my add_shortcode function nested in the my_shortcode_functions?
Does my get_shortcode_id function look like it should work?

I look forward to any help correcting my code or letting me know if I’m on the wrong track here.

Related posts

1 comment

  1. As no one has responded, I found the solution myself. My code needed a very minor adjustment in the form. I simply added <?php echo $id; ?> in the results section of the form button.

Comments are closed.