0

I'm trying to create my first wordpress plugin. I will try to explain what I'm trying to do... Hope someone can help me :)

I would like to allow a backoffice user to switch between two html codes somewhere on my site with a simple button. Ex : somewhere in my website there is a video embed code for lives. But when there is no live, the user would like to show an image instead.

So I tried to create a hook that change when the plugin is activated or desactivated. I put <?php my_switch_function(); ?> where I want the code appear and I put this in a plugin :

<?php
/**
 * Plugin Name: Europhonica Image/Vidéo switch (by Jules)
 */

 function my_switch_function() {
    do_action('my_switch_function');
}


 add_action( 'my_switch_function', 'image_my_switch_function' );


function image_my_switch_function() {
  echo '<p>Image</p>';
}



 ?>

But nothing happen... My function is shown as commented when we visit the website... Do you know how to show the echo '<p>Image</p>'; only when the plugin is activated ?

Thank you everyone !

  • That's not how "do_action" works. Rather than try to explain it here, I'll recommend that you look it up on the WordPress Codex, but it seems to me you have a lot of background to work through. In short, you need to define the context, triggering conditions, and precise functionality. It seems like you're saying you want the user to be able to click a button that replaces an inactive video feed with a static image... So, do you want the function to detect inactive video feeds and replace them? Where, exactly? Or do you want something else entirely? But that's just a beginning point... – CK MacLeod Dec 8 '16 at 16:17
0

There is many ways to do it, create a shortcode to place anywhere you want or filter the post content and add what you want.

If you plugin is not activate, this will not display,

<?php
/**
 * Plugin Name: Europhonica Image/Vidéo switch (by Jules)
 */

 // the_content method
 add_filter( 'the_content', 'image_my_switch_function_content' );
 function image_my_switch_function_content($content){
      if(is_user_logged_in()){ // change with your conditions
            $content = '<p>Image</p>'. $content
      }
      return $content;
 }
 // shortcode method
 add_shortcode('show_image', 'image_my_switch_function_shortcode');

function image_my_switch_function_shortcode($atts, $content="null") {
     if(is_user_logged_in()){ // change with your conditions
          return '<p>Image</p>';
     }

}
// Enable the use of shortcodes in text widgets.
add_filter( 'widget_text', 'do_shortcode' );

?>

The the_content filter will be embed in any post, page... depending on your conditions. With add_shortcode method the shortcode can be add manually (in the content or widget) or directly in the template with do_shortcode().

Directly in your template file :

 echo do_shortcode('[show_image]');

You can read more about these functions add_shortcode(), do_shortcode(), the_content

Hope it helps!

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.