• Resolved pixxxie

    (@pixxxie)


    I’ve created a working snippet/shortcode that accesses a table in my WordPress database to show content on a page. (Perhaps not the best use of snippets?)

    function fl_aries_co_today_shortcode() {
    global $wpdb;
    $results = $wpdb->get_results("SELECT table FROM wplx_scope_co WHERE date = CURDATE() AND sign = 'ar'");
         foreach($results as $r) {	 
              echo "<p>".$r->table."</p>";
    }
      }
    add_shortcode( 'fl_aries_co_today','fl_aries_co_today_shortcode' );

    The code executes and outputs the database result. However, it places the result at the top of my content area (and not in the text area where I paste the snippet).

    Any idea how to get the database output from the snippet to stay where I placed it in body of my content? I’m using the built-in text editor that is part of the BeaverBuilder theme.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Shortcodes should always return their output. You are using echo within your shortcode, add your output to a variable and return it.

    Thread Starter pixxxie

    (@pixxxie)

    Thanks for the reply. I’m not a coder, so I’ll have to do more research about outputting to a variable. I got this far by trial and error.

    Thread Starter pixxxie

    (@pixxxie)

    It seems that my code snippet loads too early, before the content of my webpage is loaded. Is there a way to tell the snippet not to load before the content?

    Some sort of do_action that tells my snippet to run later and allow the MySQL result to appear in the text box, where I placed the snippet?

    Thread Starter pixxxie

    (@pixxxie)

    I think I understand a little better now. Short Codes require me to convert the query into a string and then output using “result”, not echo or print?

    Plugin Author Shea Bunge

    (@bungeshea)

    Sorry, I’ve been away and so couldn’t answer this earlier. Essentially, for shortcodes, you need to make sure that you do not directly output any content (using echo or print), and instead use return to pass your content to the page.

    There are a few ways you can do this. The first is to use a variable for storing the output, and add to this variable:

    function fl_aries_co_today_shortcode() {
    	global $wpdb;
    	$results = $wpdb->get_results("SELECT table FROM wplx_scope_co WHERE date = CURDATE() AND sign = 'ar'");
    	$output = '';
    	
    	foreach ($results as $r) {
    		$output .= "<p>" . $r->table . "</p>";
    	}
    	
    	return $output;
    }
    add_shortcode( 'fl_aries_co_today','fl_aries_co_today_shortcode' );

    This is the best and fastest method, however there are some cases where you can’t store your output in a variable – such as when you are calling a function that always echos its output. In this case, you can use output buffering. Here’s how that would look in that example:

    function fl_aries_co_today_shortcode() {
    	global $wpdb;
    	$results = $wpdb->get_results("SELECT table FROM wplx_scope_co WHERE date = CURDATE() AND sign = 'ar'");
    	
    	ob_start();
    	
    	foreach ($results as $r) {
    		echo "<p>" . $r->table . "</p>";
    	}
    	
    	return ob_end_clean();
    }
    add_shortcode( 'fl_aries_co_today','fl_aries_co_today_shortcode' );
    • This reply was modified 7 years, 10 months ago by Shea Bunge.
    Thread Starter pixxxie

    (@pixxxie)

    Amazing, works perfectly. Thank you so much!

    Plugin Author Shea Bunge

    (@bungeshea)

    No problem; glad you could get it working!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Snippet/shortcode moves on content page’ is closed to new replies.