0

I have developed a short code function in function.php. What I want to do is pass the argument to that short code function so it will fetch data from database based on some query using that argument. I just want to know how to pass argument. Like I have used short code on WordPress page.

How can I pass name of the page or static text argument to the short code function in PHP? For the time being I am fetching data by using static argument.

My short code function is:

function db_short() {
    global $wpdb;
    $result = $wpdb->get_results ( "SELECT * FROM interestclass where Subject='Bakery, Pastory & Cookies' " );
}
add_shortcode( 'show_db_info', 'db_short' );

1 Answer 1

0
/* you must change your code to pass agrument in shortcode & fetch same argument in you function code like below */

/* **shortcode** */

[show_db_info name="text here"]

/* **function code** */
 
  
function db_short($atts) {
    
    $name = ($attr['name']!='' ? $attr['name'] : 'default value here if blank') ;  
    global $wpdb;

    $result = $wpdb->get_results ( "SELECT * FROM interestclass where Subject='Bakery, Pastory & Cookies' " );

}

   add_shortcode( 'show_db_info', 'db_short' );
Sign up to request clarification or add additional context in comments.

2 Comments

Can we send multiple argument in short code? @wpdevloper_j

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.