1

I'm using the following code and calling the function from the page. It is not responding.

add_shortcode('my_ttcode', 'create_my_shortcode');
function create_my_shortcode($key1, $key2){
global $wpdb;
$result = $wpdb -> get_results ( "
    SELECT * 
    FROM wp_villageleaderdb 
    WHERE VillageID = $key1 AND VillageLeaderPosition = $key2 LIMIT 0, 100
    ", 'ARRAY_N' );
var_dump($result);  
//return $result[0][1] . "<br />";  $data = '';
$i = 0;
foreach( $result as $single_result ){
$data .= $result[$i][1] . "<br />";
$i++;
}

Instead of $key1 and $key2 if the values are given, it is working ok.

The calling from the wordpress page is

[my_ttcode (V001, Sarpanch)]

What is the correct way?

I have tried but the parameter is not getting passed. here is the code.

 add_shortcode('vill_problems', 'get_villageproblems');
 function get_villageproblems($atts){ 
 extract(shortcode_atts( array( 'villid' => "V000", ), $atts ));
 global $wpdb;
 $result = $wpdb -> get_results ( "
 SELECT *
 FROM wp_villageleaderdb
 WHERE VillageID = $villid LIMIT 0, 100
 ", 'ARRAY_N' );

 $i = 0;
 foreach( $result as $single_result ){
 $data .= $result[$i][1] . "<br />"; $i++;
 }
 return $data . "<br />";
 } 
1

The following is problematic in the OP's code snippet:

  • Missing shortcode_atts() for default attribute handling.
  • Incorrectly defining the shortcode's input argument, use instead function create_my_shortcode( $attr = [], $content = null ){ ... }
  • Not escaping user input, check e.g. wpdb::prepare(). We want to avoid possible SQL injections.
  • Uses hardcoded table prefix, wp_, instead of $wpdb->prefix.
  • Fetches all fields with SELECT *. Better to fetch only what's needed.
  • Not returning the output from the shortcode's callback.
  • Incorrectly setting the shortcode's attributes, should be called from the content with e.g. [my_ttcode key1="V001" key2="Sarpanch"] but use better descriptive attributes instead of key1 and key2.
  • Maybe not using WP_DEBUG, when developing, as there would be PHP notices from eg. not initializing the $data string before appending to it.
  • Not indenting the code properly, makes it harder to maintain and understand code projects.

Check e.g. the Codex for more general info on shortcodes.

Hope it helps!

  • +1. Someone told me codex is deprecated. Glad to see you disagree :) – Jack Johansson Aug 12 '17 at 10:48
  • 1
    hehe, it's true about the reference on functions/hooks/classess etc, but I'm not sure the longer articles, like the one on the Shortcodes API, have been relocated to a newer doc home on wordpress.org, so I linked to the old Codex page in the meanwhile ;-) Also the API links on developer.wordpress.org/reference still point to the old Codex. – birgire Aug 12 '17 at 10:59
  • Thank you for the info. I was unable to bring these into. Most of the info is very cryptic for me. Can you pl give a simple example – Sriram Nadiminti Aug 12 '17 at 13:53
  • You're welcome @SriramNadiminti - did you check out the Codex article for general info? – birgire Aug 14 '17 at 19:16
  • I have tried but the parameter is not getting passed. here is the code. <code> add_shortcode('vill_problems', 'get_villageproblems'); function get_villageproblems($atts){ extract(shortcode_atts( array( 'villid' => "V000", ), $atts )); global $wpdb; $result = $wpdb -> get_results ( " SELECT * FROM wp_villageleaderdb WHERE VillageID = $villid LIMIT 0, 100 ", 'ARRAY_N' ); $i = 0; foreach( $result as $single_result ){ $data .= $result[$i][1] . "<br />"; $i++; } return $data . "<br />"; } – Sriram Nadiminti Aug 14 '17 at 23:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.