2

I'm trying to disable Jetpack Carousel on a specific post ID using the following code in my functions.php

function djcoh_disable_carousel( $value ) {

    wp_reset_query();
    if ( is_page( 614 ) ) {
        $value = true; // true to disable Carousel
    }
    // Return original or changed value
    return $value;

}

add_filter( 'jp_carousel_maybe_disable', 'djcoh_disable_carousel' );

Here's the reference for jp_carousel_maybe_disable on GitHub

It seems that I'm unable to use is_page() within functions.php - though I thought I'd be able to by using wp_reset_query() as mentioned in the codex

What am I missing?!

rmbaumer
  • 45
  • 1
  • 8
  • Why do you think you are unable to use is_page ? How have you isolated it as being this? Have you tried your code without the if clause to see if the code actually disable the carousel? – Phill Healey Feb 15 '18 at 20:59
  • I've verified that it disables the carousel on all pages without the if clause. This other SO answer lead me to believe that I can't use is_page() in functions.php https://stackoverflow.com/a/22070503/3650556 – rmbaumer Feb 15 '18 at 21:03
  • also, why are your using `wp_reset_query();`? its need to use after `query_posts`, – Samvel Aleqsanyan Feb 15 '18 at 21:03
  • I got that from here https://premium.wpmudev.org/forums/topic/how-to-disable-jetpack-carousel-on-one-page and also saw it referenced on the WP codex for is_page() – rmbaumer Feb 15 '18 at 21:06
  • As @SamvelAleqsanyan says reset is usually used after a custom query to kick everything back to the loop query. – Phill Healey Feb 15 '18 at 21:07
  • This code is intended for use as a simple plugin, have you tried it as such, just to make sure it works as expected? If, so we can then narrow this down to the "is_page" function. – Phill Healey Feb 15 '18 at 21:10
  • Yes. Tried it 'As is' as a plugin instead of in functions.php using the original is_front_page() - it did not disable the carousel on the front page. – rmbaumer Feb 15 '18 at 21:15
  • Also, tried it as a plugin but using is_page() - same deal – rmbaumer Feb 15 '18 at 21:16
  • Ever figure this out? – Brendan Sep 14 '18 at 03:59
  • Unfortunately no. I ended up working around it though by using the WP function is_page() to conditionally add the following jQuery to the page where I needed to disable the carousel: `jQuery('.gallery-item').on('click', function(e) {e.stopPropagation();});` – rmbaumer Sep 14 '18 at 08:40
  • @MarkAnderson Did my answer or one of the other help you? If so please mark the appropriate answer as the accepted solution. Or, post an update and we'll try to help. Thanks. – Phill Healey Jan 13 '20 at 11:22

3 Answers3

1

The code you have is from a tutorial which is intended for running as a simple plugin. The reason your code doesn't currently work is because you are using it in the functions.php.

In it's current form your function is called as soon as it is read as part of the functions.php file. This is usually some time before the page is formed, and so you can't grab the page id with is_page{}.

Instead you should query the page and get it's id as follows:

function djcoh_disable_carousel( $value ) {

//get the global
global $post
echo "TEST PAGE ID: ".$post->ID;
//wp_reset_query();
if ( $post->ID == 614 ) {
    $value = true; // true to disable Carousel
}

wp_reset_query();
// Return original or changed value
return $value;

}

add_filter( 'jp_carousel_maybe_disable', 'djcoh_disable_carousel' );

if that doesn't work try this:

function djcoh_disable_carousel( $value ) {

//get the global
global $wp_query;
$post_ID = $wp_query->post->ID;
echo "TEST PAGE ID: ". $post_ID;
//wp_reset_query();
if ( $post_ID == 614 ) {
    $value = true; // true to disable Carousel
}

wp_reset_query();
// Return original or changed value
return $value;

}

add_filter( 'jp_carousel_maybe_disable', 'djcoh_disable_carousel' );

If none of the above work then your script is being called far too early in the process to grab the page id. So, the easiest option would be to simply place this script in it's own .php file and then upload that to the plugins root folder. Then activate it from the plugins menu.

The final option would be to create this as a filter or script and add the function call in the actual page template.

Phill Healey
  • 3,084
  • 2
  • 33
  • 67
  • Thanks for the answer! Still same issue though - it works on all pages without the if clause - but as soon as I add `if ( $post->ID == 614 )` or `is_page` it stops working – rmbaumer Feb 15 '18 at 21:25
  • Ok, try my updated code. If it doesn't work are you seeing the page ID output via the echo? – Phill Healey Feb 15 '18 at 21:29
  • Still not working. It does echo out "TEST PAGE ID: " but not the page id – rmbaumer Feb 15 '18 at 21:35
  • Same! added a semicolon to `global $post;` but still not echoing the page id – rmbaumer Feb 15 '18 at 21:41
  • Just added another 2 final options. – Phill Healey Feb 15 '18 at 21:46
  • Still no-go, but thanks Phill. I don't quite follow the 'final option' - could you elaborate on that? – rmbaumer Feb 15 '18 at 21:54
  • Which, the simple plugin (easiest option by far) or adding the function to your page template? – Phill Healey Feb 15 '18 at 22:00
  • Both. I tried the simple plugin and tried adding the function directly to the page.php template (where I tried adding the function before, within, and after the main while loop). – rmbaumer Feb 15 '18 at 22:15
  • Ok, so take your original code, place it in a file named mycarouselfix.php. Then add '' now upload it to your plugins root eg 'wp-content' > 'plugins'. Now activate the plugin and it should work. – Phill Healey Feb 15 '18 at 22:23
  • Did anyone ever verify that this works? I can't get it to work. I tried 1. adding it in my site-specific plugin, 2. adding to functions.php, 3. adding as a separate plugin. 4. I can get the function to fire (and detect the front page) if I use a different filter like `wp`, but that doesn't disable carousel. 5. I tried adding the filter in a few different spots in my page templates, but no good. 6. I tried applying the filter with the function always returning true, and it disables the jetpack carousel. All of the pieces are there, but this hook seems to fire before WP knows what page it's on. – Brendan Sep 14 '18 at 03:21
1

I managed this by using REQUEST_URI within a plugin file:

<?php

// No direct access
if ( ! defined( 'ABSPATH' ) ) exit;

if ( $_SERVER["REQUEST_URI"] === '/PAGE-SLUG/' ) {
    add_filter( 'jp_carousel_maybe_disable', '__return_true' );
}

Change PAGE-SLUG for your slug and you are all set.

You can find info on REQUEST_URI in PHP's manuals:

'REQUEST_URI' The URI which was given in order to access this page; for instance, '/index.html'.

ecotechie
  • 71
  • 6
0

It seems simplest to conditionally dequeue the Jetpack carousel script and stylesheet. The conditionals that you would typically use to control output would be available at the point in the request when the wp_footer action fires.

add_action( 'wp_footer', function() {
    if ( is_page( $page ) ) {
        wp_dequeue_script( 'jetpack-carousel' );
        wp_dequeue_style( 'jetpack-carousel' );
    }
}

Be certain to modify the is_page function to include the $page parameter or the condition will match all pages. Place the code in your theme's functions.php file and the Jetpack carousel should be disabled.

Eric Kyle
  • 1
  • 1