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.
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
}
I'm writing a simple plugin for wordpress that changes a single word on a page or a post to make it bold.
For example: vlbs -> vlbs
It works fine for normal Wordpress pages and posts with this code:
defined('ABSPATH') or die('You can\'t enter this site');
class VLBS {
function __construct() {
}
function activate() {
flush_rewrite_rules();
}
function deactivate() {
flush_rewrite_rules();
}
function unstinstall() {
}
function new_content($content) {
return $content = str_replace('vlbs','<strong style="color:#00a500">vlbs</strong>', $content);
}
}
if(class_exists('VLBS')){
$VLBS = new VLBS();
}
add_filter('the_content', array($VLBS, 'new_content'));
//activation
register_activation_hook(__FILE__, array($VLBS, 'activate'));
//deactivation
register_deactivation_hook(__FILE__, array($VLBS, 'deactivate'));
However, it does not work on a page built with Yootheme Pro Pagebuilder. Whatever is done within the function new_content() is processed after the content has already been loaded. Thus, I cannot manipulate it before it is displayed to the user.
So the question would be: How can I get the content of a page before it is displayed? Is there an equivalent to Wordpress' 'the_content'?
Any help is really appreciated! Thank you very much in advance.
Best regards
Fabian
Yootheme: 1.22.5
Wordpress: 5.2.4
PHP: 7.3
Browser: Tested on Chrome, Firefox, Edge, Internet Explorer
In your code, do you are sure it's the good usage of add_filter content ?
In the doc, the 2nd parameter is string, not array:
add_filter( 'the_content', 'filter_the_content_in_the_main_loop' );
function filter_the_content_in_the_main_loop( $content ) {
// Check if we're inside the main loop in a single post page.
if ( is_single() && in_the_loop() && is_main_query() ) {
return $content . esc_html__("I'm filtering the content inside the main loop", "my-textdomain");
}
return $content;
}
In wordpress, the_content function display the content. There is an other function for get_the_content
Go to your page file and get the content. You can use str_replace and echo the new content after.
Example single.php :
if ( $query->have_posts() ) :
while( $query->have_posts() ) : $query->the_post();
$content = get_the_content();
$new_content = str_replace(search, replace, $content);
echo $new_content;
endwhile;
endif;
If it is not possible for you, try to use the output buffer functions. If you need use this functions, I say it and I developpe more this part. But test the solution above before.
Oh, and it exist a special community for WP where your question will more pertinent : https://wordpress.stackexchange.com/
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.
I have a wordpress website in which there is a custom theme where I can create template parts for each webpage. I want to include/inject a specific class (ex: abc ) to the body tag in header.php when a specific page template (ex: abc.php) loads.
First I used the code bellow in header.php it did not work.
<body <?php if ( is_page_template( 'template-parts/page/abc.php' ) ) { body_class( 'abc' ); } else { body_class(); } ?>>
Then I added bellow code to functions.php but it is not working either.
add_filter( 'body_class','abc_class' );
function abc_class( $classes ) {
if ( is_page_template( 'template-parts/page/abc.php' ) ) {
$classes[] = 'abc';
}
return $classes;
}
I do not understand what I am doing wrong here. How can I fix this?
First of all the is_page_template() function works for WP templates, I mean which have in the beginning of the file
/*
* Template name: Your Super Template
*/
So, the function returns true is a page with this template is actually displaying. I will tell you more, you even do not have to add custom body classes because function body_class() adds specific unique classes for each page template :)
Second, the page templates can be in the theme directory itself or in 1-level subdirectory, so
<body <?php if ( is_page_template( 'abc.php' ) ) { body_class( 'abc' ); } else { body_class(); } ?>>
or
<body <?php if ( is_page_template( 'page-templates/abc.php' ) ) { body_class( 'abc' ); } else { body_class(); } ?>>
Your template in 2-level sub-directory, so it is not actually a Page Template, it is a template part.
So, in two words, the function is_page_template() is for Page Templates but not for template parts
I was able to add the following in functions.php so that it takes the slug and inject that to the body class every time a specific page loads.
add_filter( 'body_class', function( $classes ) {
if ( is_page() ){
$slug = get_queried_object()->post_name;
return array_merge( $classes, array( $slug ) );
}
return $classes;
} );