0

I am working on a wordpress plugin. In plugin, a user first add a slider and then add images of relevant slider.

First i make this with shortcode. User enter the short name of the slider in the shortcode and images slides of relevant slider like this [foo_slider slider=slider_one] or [foo_slider slider=slider_two].

Now i "also" want the snippet, that user can add snippet else shortcode in the code like this echo wp_foo_slider(slider_two). But i dont get that.

Please guide me, how can i do this.

Here is my Code That works for shortcode:

<?php
function wp_foo_sliders($atts) {
    global $wpdb;
    $tbl_sliders = $wpdb->prefix . "foo_sliders";

    ob_start();
    extract(shortcode_atts(array(
        'slider' => '',
                    ), $atts));

    $get_sliders = $wpdb->get_results("Select * From $tbl_sliders Where slider_shortname = '$slider'");
?>
    <div class="foo_main_slider">
    <?php
        foreach ($get_sliders as $get_slider) {
            $slider_id = $get_slider->slider_id;
    ?>
    <div class="foo_slider_img">
    <?php
        $get_slider_image = $wpdb->get_results("Select * From ".$wpdb->prefix."foo_images Where 

slider_id = $slider_id Order By image_order ASC");

        foreach ($get_slider_image as $foo_img) { 
    ?>
        <img src="<?php echo $foo_img->image_path . $foo_img->image_name; ?>" alt="">
     <?php
        }
        }
        return ob_get_clean();
}

add_shortcode("foo_slider", "wp_foo_sliders");
?>

I also try this by my own <?php echo wp_foo_sliders("slider_two") ?> or <?php echo wp_foo_sliders(slider_two) ?>, in code and when i refresh the browser only <div class="foo_main_slider"> </div> appears and no images show.

Edit: I want that user can use short code <?php echo do_shortcode('[foo_slider slider=slider_one]'); ?> or user can use snippet <?php echo wp_foo_sliders("slider_two") ?>, only shortcode is working, snippet not work.

What i make mistake please help me.

9
  • You want to use that short code in your PHP file manually. Is that the case ? Commented Oct 28, 2014 at 6:16
  • Your shortcode is called foo_slider, not wp_foo_sliders Commented Oct 28, 2014 at 6:39
  • wp_foo_sliders() is my function name where i call images of a slide Commented Oct 28, 2014 at 6:43
  • 1
    True, but this function is modified through the shortcode. Did you check out the Shortcode API in the codex how to use a shortcode and how to use the attributes to modify what the shortcode returns Commented Oct 28, 2014 at 6:52
  • hmmmmmm no, i dont check this Commented Oct 28, 2014 at 6:53

2 Answers 2

1

When you call the shortcode function directly, you are passing a string to it. When you use the shortcode way WordPress will convert the arguments into an associative array.

Try refactoring your code

if( is_array( $atts ) ) {
    //Called using shortcode so $atts is an array
    extract(shortcode_atts(array(
        'slider' => '',
                    ), $atts));

} else {
    //Function called directly so $atts is a string
    $slider = $atts;
}
Sign up to request clarification or add additional context in comments.

Comments

0

So i get my solution by my own.

I make a new function for this:

<?php
function foo_sliders($foo_short_name) {
    global $wpdb;
    $tbl_sliders = $wpdb->prefix . "foo_sliders";

    $get_sliders = $wpdb->get_results("Select * From $tbl_sliders Where slider_shortname = '$slider'");
?>
    <div class="foo_main_slider">
    <?php
        foreach ($get_sliders as $get_slider) {
            $slider_id = $get_slider->slider_id;
    ?>
    <div class="foo_slider_img">
    <?php
        $get_slider_image = $wpdb->get_results("Select * From ".$wpdb->prefix."foo_images Where 

slider_id = $slider_id Order By image_order ASC");

        foreach ($get_slider_image as $foo_img) { 
    ?>
        <img src="<?php echo $foo_img->image_path . $foo_img->image_name; ?>" alt="">
     <?php
        }
        }
    ob_start();
    return $foo_short_name;
    return ob_get_clean();
}

?>

And in theme code:

<?php foo_sliders(short_name) ?>

and its work great

Comments

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.