1

I wanted to run a basic php condition to my footer if correctly activated at certain page.

Code is:

if ( $photoSwipe == 1 ) {
  get_template_part( _ps );
}

So, if i am going to activate this plugin, i can trigger the $photoSwipe = 1 to my certain page that will enable the get_template_part(_ps) to my footer area.

1 Answer 1

1

This would be a viable way to do it from a plugin, however it seems like it would make more sense in a template file.

If it's a universal option, you would create an option either through a custom settings page or just by initializing on plugin activation in the activation hook (if its a static value):

function wpse_photoswipe_activate_plugin() {
    $page_id = 123; // This could be an ID, or if you wanted, a template name
    add_option( 'wpse_activate_photoswipe', $page_id );
}
register_activation_hook( __FILE__, 'wpse_photoswipe_activate_plugin' );

function wpse_photoswipe_deactivate_plugin() {
    delete_option( 'wpse_activate_photoswipe' );
}
register_deactivation_hook( __FILE__, 'wpse_photoswipe_deactivate_plugin' );

Then in your footer template, you could use something like:

global $post;
if ( get_option( 'wpse_activate_photoswipe' ) === $post->ID ) {
    get_template_part( _ps );
}

However, if you want my opinion on the easiest way to do this, since it sounds like you're already editing template files, is to just update your existing code to:

if ( is_page( $page_id ) ) {
    get_template_part( _ps );
}

There are even more ways to do what you're suggesting but given the information you provided (existing theme hooks or wp_footer, for example), this should steer you in the right direction.

2
  • this code, solved my case... and you are correct, each template files are representing the PhotoSwipe template and others are not... Cause, I don't want to make the PhotoSwipe template visible on every page if it is not in use. Question on the $page_id, can i use array here? Mar 8, 2017 at 3:17
  • @iMarkDesigns sure can. is_page() will take "Page ID, title, slug, or array of such" developer.wordpress.org/reference/functions/is_page Mar 21, 2017 at 20:08

Your Answer

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

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