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?!
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.
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'.
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.
Related
I want to remove the information about an installed plugin from the WordPress dashboard plugins page. I have written the following code, but it doesn't work!
please guide me?
add_filter( 'all_plugin', 'remove_plugins');
function remove_plugins($plugins)
{
if(is_plugin_active('/woocommerce-checkout-manager/woocommerce-checkout-manager.php')) {
unset( $plugins['woocommerce-checkout-manager.php'] );
}
return $plugins;
}
I added this code to my template function file but it still doesn't work.
Use the filter below to delete the information of the plugin installed in WordPress and the WordPress plugins page.
Note that in the first value, put the folder and the main file of the plugin, and in the second value, only the main file of the plugin without adding the folder.
add_filter(
'all_plugins',
function ( $plugins ) {
$shouldHide = ! array_key_exists( 'show_all', $_GET );
if ( $shouldHide ) {
$hiddenPlugins = [
'woocommerce-checkout-manager/woocommerce-checkout-manager.php',
'woocommerce-checkout-manager.php',
];
foreach ( $hiddenPlugins as $hiddenPlugin ) {
unset( $plugins[ $hiddenPlugin ] );
}
}
return $plugins;
}
);
I am trying to add the below code to a page.php file but call it from a custom plugin. At the moment, I have modified the theme's page.php but want to move custom code to a standalone plugin. Your help is appreciated.
// Check if the user is actually logged in first & if they have the ability to publish posts
if ( is_user_logged_in() && current_user_can('listee') || current_user_can('administrator') ) { // Execute code if user is logged in
acf_form_head();
wp_deregister_style( 'wp-admin' );
}
There are a number of ways to do it.
1. You could use an action hook.
On page.php:
// Hook where you dsire the php to go
do_action( 'custom_action_hook' );
In functions.php:
// function to hook into the custom action hook
function namespace_run_code() {
// run code here
}
add_action( 'custom_action_hook', 'namespace_run_code' );
2. Use conditional template functions
In header.php:
if ( is_page_template( 'page.php' ) ) {
// run php code if on page.php
}
How do I automatically include the following PHP script in my WordPress posts?
<?php if ( function_exists( 'ADDTOANY_SHARE_SAVE_KIT' ) ) { ADDTOANY_SHARE_SAVE_KIT(); } ?>
I am not familiar with PHP and I have assumed that the above is a code from a plugin I am using to show social sharing buttons.
I was hoping someone may have dealt with adding elements into the excerpt automatically before and would have some code I could copy into my child theme functions.php?
AddToAny Share Buttons plugin have options Display at the bottom of excerpts
or you can add code
function add_excerpt_social( $excerpt ) {
if ( function_exists( 'ADDTOANY_SHARE_SAVE_KIT' ) ) { return $excerpt.ADDTOANY_SHARE_SAVE_KIT(array("output_later" => true)); }
else { return $excerpt; }
}
add_filter('get_the_excerpt', 'add_excerpt_social');
to your theme functions.php file.
I understand that the Wordpress global $post may not work until a certain point because it hasnt been loaded, but Im wondering is there a workaround for this.
Basically Im building a plugin specifically for a client. At the moment the variable is showing nothing.
This code is in a plugin in the plugins folder.
Im looking to make sure it only loads javascript (Ive left out that bit) when on specific pages (selected by the user), at the moment its loading on all pages.
My other option is to do it all on a php template, but to be honest I wanted to write it as a plugin with a view to customizing it for more generic use in the future, plus I have little experience with plugins so Im trying to improve that side of things also.
function include_js() {
global $post;
print_r($post);
if(is_object($post) && $post->ID == 14 ){
// do stuff
wp_enqueue_script('include-map', plugin_dir_url( __FILE__ ) . 'map.js');
}
}
add_action( 'init', 'include_js' );
EDIT: I realised my main issue is that I want to include the javascript and because of that I need wp_enqueue_script , but I can only seem to get that work if you use the init action, which happens before the loop.
After seeing your edit, try hooking to wp_enqueue_scripts instead of init. Like this:
function include_js() {
global $post;
print_r( $post );
if ( is_object( $post ) && $post->ID == 14 ) {
// do stuff
wp_enqueue_script( 'include-map', plugin_dir_url( __FILE__ ) . 'map.js' );
}
}
add_action( 'wp_enqueue_scripts', 'include_js' );
Ref: http://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts
You should test $wp_query->queried_object_id instead of $post->id:
function include_js() {
global $wp_query;
if ( $wp_query->queried_object_id == 14 ) {
// do stuff
//...
}
}
add_action( 'wp_enqueue_scripts', 'include_js' );
There is no need for any globals etc. You can simply use is_single($post) to check if you are on a specific single post and then enqueue your script. You should always always use the wp_enqueue_scripts hook to hook your function to to enqueue scripts and styles
function include_js() {
if(is_single(14)) {
// do stuff
wp_enqueue_script('include-map', plugin_dir_url( __FILE__ ) . 'map.js');
}
}
add_action( 'wp_enqueue_scripts', 'include_js', 999 );
Remeber also, always add priority when adding custom scripts/styles. See add_action( $hook, $function_to_add, $priority, $accepted_args )
I'm new to Wordpress & PHP, so kindly excuse the naivety of the question, if any.
I'm building a plugin where I need to select values from the database and create a new HTML page with the values. I'm using a custom template file.
What I've done till now
Extracted the values from database
Load & display my own template in my plugin file
add_action( 'init', 'leb_add_endpoint' );
function leb_add_endpoint()
{
add_rewrite_endpoint( 'result', EP_PERMALINK );
}
add_action( 'template_include', 'leb_render_template' );
function leb_render_template($default_template) {
//some code removed for brevity
if ( ! is_singular() || !isset($wp_query->query_vars['result']) )
{
return $default_template;
}
$sample_result = $wpdb->get_var($wpdb->prepare($sql));
$default_template = plugin_dir_path(__FILE__) . '/my-custom-template.php';
return $default_template;
}
The content of my-custom-template.php is as follows
<?php
/* Template Name: My Template*/
echo '<h1>Testing</h1>';
?>
The page gets displayed without any problem. All I want is to insert $sample_result and other similar results pulled form database into my-custom-template.php
I need to generate dynamic pages based on values pulled from DB. So each time, the final page created might be different. E.g. if one hits www.example.com/sample-post/result, a page will be shown with values pulled from the DB. If one hits www.example.com/another-sample-post/result, a different page will be shown with different values. Both these pages will have the same design, only a few values will be different. This is what I'm trying to achieve.
How can I do that? Please help me. I'm stuck. :(
You need to define "result" in query vars.
Use EP_ROOT Endpoint Mask ( result/{var} is located in the root )
Inside template_include hook, you can find result value inside $wp_query object.
I've already tested the code
// Add a new var to query vars
function result_add_query_vars( $vars ){
$vars[] = 'result';
return $vars;
}
add_filter( 'query_vars', 'result_add_query_vars' );
// Add endpoint
function result_add_endpoint() {
add_rewrite_endpoint( 'result', EP_ROOT );
}
add_action( 'init', 'result_add_endpoint');
// change the template
function result_render_template($template)
{
global $wp_query;
if ( array_key_exists( 'result', $wp_query->query_vars ) ) {
$result = get_query_var('result');
$new_template = plugin_dir_path(__FILE__) . '/my-custom-template.php';
return $new_template;
} else {
return $template;
}
}
add_action( 'template_include', 'result_render_template' );
Now you can retrieve the query var in your custom template
/*
* Template Name: My Custom Template
*/
$result = get_query_var('result');
echo $result;
Well why don't you use $wp_query Inside your my-custom-template.php
<?php
/* Template Name: My Template*/
global $wp_query;
echo '<pre>';
print_r($wp_query); // Use this in case you want to see what else do you have with you.
echo '<pre/>';
// Now you can use $wp_query to build your dynamic query at run time.
// This will allow you to perform task at run time
?>
To Retrieve Meta
If you have saved something as a meta then
<?php
$meta_values = get_post_meta( $post_id, $key, $single );
?>
To Retrieve Child Post
If you want to retrieve child posts then
<?php
if ( have_posts() ) :
// Start the Loop.
while ( have_posts() ) : the_post();
// Do your stuff here such as below
the_title();
the_content();
endwhile;
else:
echo 'No Post Found';
endif;
?>
1) Write this function in function.php in your active template.
function leb_render_template() {
//some code removed for brevity
//$sql = 'YOUR_QUERY_CODE'
$sample_result = $wpdb->get_var($wpdb->prepare($sql));
return $my_template;
}
add_action('wp_ajax_leb_render_template', 'leb_render_template');
add_action('wp_ajax_nopriv_leb_render_template', 'leb_render_template');
2) Call function in your custom template.
<?php
/* Template Name: My Template*/
echo '<h1>Testing</h1>';
$result = leb_render_template();
print_r($result); // Print Your function output.
?>