Cancel the AddThis Wordpress plugin on certain posts - php

I have a Wordpress site that utilizes multiple custom post types to achieve different goals. I also use the AddThis plugin to append social sharing buttons to the top of the posts. However, I have one type of custom post that I do not the plugin to append the AddThis sharing buttons to. Is there a way to hook into the AddThis plugin to get it to return false or something given a certain condition?
Maybe something like:
if($post_type === "no-addthis"){
addthis_plugin_hook(false);
}
I totally realize that's not a valid way to do it, but does anyone know of something similar?
Thanks in advance.

The following will remove all AddThis filters operating on the current post:
if( get_post_type( get_the_ID() ) == 'your-post-type' ) {
remove_filter('the_content', 'addthis_display_social_widget', 15);
remove_filter('the_content', 'addthis_script_to_content');
remove_filter('get_the_excerpt', 'addthis_display_social_widget_excerpt', 11);
remove_filter('get_the_excerpt', 'addthis_late_widget', 14);
remove_filter('wp_trim_excerpt', 'addthis_remove_tag', 11, 2);
}
I'd advise running this code right before any call to the_content() or the_excerpt() ( or similar function prefixed with get_ ) in your custom post type template.
Note: I haven't tested this and I've never used the AddThis plugin. I just peeked at the source code just now. :)

Newest version I do the following in functions.php
add_action( 'init', 'remove_addthis' );
function remove_addthis(){
remove_action( 'wp_head', 'addthis_add_content_filters');
}

Related

Removing the WooCommerce sidebar the correct way - TwentySeventeen?

I'm working on a project that is running of a child theme of TwentySeventeen and whilst the rest of the site doesn't have a sidebar, WooCommerce seems to have it.
For example, the shop page has it - I have tried a few things already and none work without caveats or didn't work at all:
I tried copying archive-product.php to my theme dir in woocommerce/archive-product.php and removing the below:
do_action( 'woocommerce_after_main_content' );
This didn't work.
I then tried doing:
remove_action('woocommerce_sidebar','woocommerce_get_sidebar',10);
...this didn't work either.
I found this answer and it worked, but didn't make the page full width (still had space for the sidebar) and a comment on the answer noted using that method isn't a great idea.
I also found this answer but it involves adding CSS, something I'd like to avoid as it isn't the most robust method in-case class names change in the future etc...
Isn't there a proper way of doing this without potential side affects?
Please, add this code to your functions.php
For remove only woocommerce side bar
function disable_woo_commerce_sidebar_mms() {
remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10);
}
add_action('init', 'disable_woo_commerce_sidebar_mms')
for remove all side bars
function remove_sidebar_mms() {
return false;
}
add_filter( 'is_active_sidebar', 'remove_sidebar_mms', 10, 2 );
OR
You can try this with to increase the priority hope fully its work
remove_action('woocommerce_sidebar','woocommerce_get_sidebar',25);
With the help of Mannu saraswat's answer and some fiddling around I came up with a solution:
// Remove the sidebar
add_action('get_header', 'blm_wc_remove_sidebar_check', 10);
// Removes the sidebar
function blm_wc_remove_sidebar($index) {
return false;
}
// Check to see if we're on a WooCommerce page and if so, remove the sidebar
function blm_wc_remove_sidebar_check() {
if ( is_woocommerce() ) {
add_filter('is_active_sidebar', 'blm_wc_remove_sidebar', 10, 1);
}
}
This avoids having to do the is_active_sidebar check / filter addition on non-WooCommerce pages.
Maybe there is a cleaner way to do this, but this worked for me.

Featured Images in WP not showing

Added the line of code in my function.php file
add_theme_support('post-thumbnails');
After going saving, and looking at my screen options, Featured images still wasn't showing. I even went as far back to check if my Custom Fields was correct and I had it unmarked to show
First: (I assume it's a typo in your description): Change function.phpto functions.php
Second: Try to call add_theme_support('post-thumbnails') within a function attached to the after_setup_theme hook as shown in the documentation.
// Register Theme Features
function custom_theme_features() {
// Add theme support for Featured Images
add_theme_support( 'post-thumbnails' );
}
// Hook into the 'after_setup_theme' action
add_action( 'after_setup_theme', 'custom_theme_features' );
I hope these steps will fix your issue, otherwise it would be necessary to provide more of your code
hi #EliCollins Use this code. It should work
add_action( 'after_setup_theme', 'theme_setup' );
function theme_setup() {
add_theme_support('post-thumbnails' );
}
I actually found out the answer just last night. In the CPT UI on the left hand side there is a settings area "Click headings to reveal available options.", click on the settings caret and at the bottom under supports select Featured Images. And then of course going into your function.php and adding
add_theme_support('post-thumbnails' );

How To Make A Function That Links To a Certain File In WordPress

Ok, so im working on a theme that I want to have different styles and php files depending on which design the user has chosen. How would I do this? The best example I can give is having a get_template_part function that changes directory based on the users selection of design. Heres my idea of how the code would kinda look.
<?php /*custom_function*/(/*Global Path variable which changes*/, '/filename') ?>
The x theme has like stacks I think which might be a similar idea. Thanks.
Maybe something like this in the function.php: (just an example)
<?php
define("DESIGN", "flat");
if(DESIGN == "flat"){
add_action( 'wp_enqueue_scripts', 'asd_scripts' );
}elseif(DESIGN == "notflat"){
// other add_action( 'wp_enqueue_scripts', '...' );
}
function asd_scripts() {
// load bootstrap js
wp_enqueue_script('asd-bootstrapjs', get_template_directory_uri().'/includes/resources/bootstrap/js/bootstrap.js', array('jquery') );
}
?>
You need to use the theme customization api to achieve this. For example you would use the customize_register hook:
function mytheme_customize_register( $wp_customize )
{
//All your sections, settings, and controls will be added here
}
add_action( 'customize_register', 'mytheme_customize_register' );
This hook allows you access to the $wp_customize object.
There are existing examples in the WordPress docs, this should get you started with what you would like to do.

Wordpress: Disable Tags only for Posts

I am currently developing a theme for Wordpress 3.8.1. As my theme will not display any tags, so I want do disable them (only from the posts, not from custom post types). But how do I do this? I have tried this, but apparently, it does nothing:
register_taxonomy('post_tag', null);
To be clear: I do not just want to hide the tags in the template files, but I want to disable them completely, so in the backend, there is no menu item for tags under posts.
Is it even possible? I hope so. Thanks for your help!
Update
Additionally, I have tried the following, without any effect:
register_taxonomy('post_tag', array());
and
global $wp_taxonomies;
$taxonomy = 'post_tag';
if(taxonomy_exists($taxonomy))
unset($wp_taxonomies[$taxonomy]);
Both remove the tags box while editing a post, but there still is the link in the menu pointing to the list of tags!
As of WordPress 3.7, there is an unregister_taxonomy_for_object_type function available for just this sort of thing.
In your case:
// Remove tags support from posts
function myprefix_unregister_tags() {
unregister_taxonomy_for_object_type('post_tag', 'post');
}
add_action('init', 'myprefix_unregister_tags');
View the documentation for this function here.
Paste this code into your functions.php
add_action( 'admin_menu', 'myprefix_remove_meta_box');
function myprefix_remove_meta_box(){
remove_meta_box( 'tagsdiv-post_tag','post','normal' );
}
tags meta box has a class of tagsdiv-post_tag, so this will remove the tags meta box
OR
add_action('init', 'remove_tags');
function remove_tags(){
register_taxonomy('post_tag', array());
}
if you completely want to remove it
The best solution to disable rewrite rules for tags in posts for me was to use the post_tag_rewrite_rules filter hook.
add_filter( 'post_tag_rewrite_rules', '__return_empty_array' );

Using variables in the URL on wordpress menu link

I am using wordpress wp-admin to create the menu. It is under Appearance/menu.
I have a link that points to /members/ but what I really need is a link to /members/$logged_user...
For example /members/user_1 or /members/user_2.
How can I do that?
I dont know if it is important, but I am using buddypress plugin.
I have written a short script for adding dynamic buddypress links in wordpress menu. Hope it helps
I have added custom links in wordpress menu replacing username in link with --username--
example
http://website_name.com/members/--username--/messages/
then add this code in function.php
add_filter( 'nav_menu_link_attributes', 'menu_override', 10, 3 );
function menu_override( $atts, $item, $args ) {
$user = wp_get_current_user();
$newlink = str_replace("--username--", $user->user_login, $atts[href]);
$atts[href] = $newlink;
return $atts;
}
The default menu doesn't really have that option. But you can make your own walker function, searching for the keyword and replacing it with the current user.
See http://codex.wordpress.org/Function_Reference/wp_nav_menu#Using_a_Custom_Walker_Function
But it would probably be faster and more manageable to just place the link outside the menu call with some static html and the needed php.
You can use the BuddyPress Custom Profile Menu plugin to get close to this functionality, otherwise you will have to write custom code that uses actions/filters of wp_nav_menu().

Categories