How to identify single instance of Wordpress default text widget? - php

I wanted to use the default WordPress text widget to the sidebar of pages where the title of the widget would dynamically change to add the title of the pages.
So I used the following code in my functions file:
function my_widget_title($title, $instance, $id_base) {
if ( is_singular() && 'text' == $id_base) {
return get_the_title($post->ID).__(' Custom Tour Inquiry');
}
else {
return $title;
}
}
add_filter ( 'widget_title' , 'my_widget_title', 10, 3);
This surely does what it is supposed to do, but it changes the titles of all other text widgets I try to use as well.
Is there any way I can pinpoint to one particular widget where this code will apply while the other text widgets will "behave normally"?

You could give the widget a specific name like "My Dynamic Text Widget". Then add detection of that title as a condition in your code like the following:
function my_widget_title($title, $instance, $id_base) {
if ( is_singular() && 'text' == $id_base && $title == 'My Dynamic Text Widget') {
return get_the_title($post->ID).__(' Custom Tour Inquiry');
}
else {
return $title;
}
}
add_filter ( 'widget_title' , 'my_widget_title', 10, 3);

Please replace your code with following one. I just added global $post to your code to get access to the specific post.
function my_widget_title($title, $instance, $id_base)
{
global $post;
if ( is_singular() && 'text' == $id_base) {
return get_the_title($post->ID).__(' Custom Tour Inquiry');
}
else
{
return $title;
}
}
add_filter ( 'widget_title' , 'my_widget_title', 10, 3);
Hope this will work for you as well.

Here is complete solution according to your situation. Kindly replace your old code with this new code and get your required result.
This will add checkbox into text widget and if you check this it will replace the title otherwise normal title will be render.
function kk_in_widget_form($t,$return,$instance){
if($t->id_base == "text"){
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '', 'float' => 'none') );
?>
<p>
<input id="<?php echo $t->get_field_id('change_title'); ?>" name="<?php echo $t->get_field_name('change_title'); ?>" type="checkbox" <?php checked(isset($instance['change_title']) ? $instance['change_title'] : 0); ?> />
<label for="<?php echo $t->get_field_id('change_title'); ?>"><?php _e('Change Title'); ?></label>
</p>
<?php
$retrun = null;
}
return array($t,$return,$instance);
}
function kk_in_widget_form_update($instance, $new_instance, $old_instance){
$instance['change_title'] = isset($new_instance['change_title']);
return $instance;
}
add_action('in_widget_form', 'kk_in_widget_form',5,3);
//Callback function for options update (priorität 5, 3 parameters)
add_filter('widget_update_callback', 'kk_in_widget_form_update',5,3);
function my_widget_title($title, $instance, $id_base) {
if ( is_singular() && 'text' == $id_base && $instance['change_title']) {
return get_the_title($post->ID).__(' Custom Tour Inquiry');
}
else {
return $title;
}
}
add_filter ( 'widget_title' , 'my_widget_title', 10, 3);
Here is backend checkbox screen

Related

Add additional empty attribute to existing HTML element

I try to include content tracking to a site with the Matomo Tagmanager. According to their documentation this should be in the form of
<a href="/random" class="tfs-highlight-button"
data-track-content
data-content-name="Random click"
data-content-piece="Random newsletter">
Click for random
</a>
I want to add this through my functions.php with the following code
add_filter( 'nav_menu_link_attributes', function ( $atts, $item, $args ) {
if ( 'tfs-highlight-button' === $item->classes[0] ) {
$atts['data-track-content'] ;
$atts['data-content-name']="Random click";
$atts['data-content-piece']="Random newsletter";
}
return $atts;
}, 10, 3 );
Unfortunately the attribute data-track-content isn't added to the HTML element. How do I add an "empty" data-attribute to the element? I already tried $atts['data-track-content']='' and $atts['data-track-content']=NULL but this didn't work.
Wordpress removes empty/falsy attributes. I would do something like this:
add_filter('nav_menu_link_attributes', function ($atts, $item, $args) {
if (in_array('tfs-highlight-button', $item->classes)) {
$atts['data-track-content'] = '1';
$atts['data-content-name'] = 'Random click';
$atts['data-content-piece'] = 'Random newsletter';
}
return $atts;
}, 10, 3);
add_filter('walker_nav_menu_start_el', function($item_output, $item) {
if (in_array('tfs-highlight-button', $item->classes)) {
$item_output = str_replace('data-track-content="1"', 'data-track-content', $item_output);
}
return $item_output;
}, 10, 2);

Add Text to Wordpress Post Title, Exclude From Menu

I am using the Avada theme for WordPress and wanted to add "..." after each WordPress title, but the code I found adds "..." after the menu text. How can I exclude the menu from being added as well?
/* Adds ... After Blog Titles */
function add_suffix_to_title($title, $id = null){
if (! is_admin()) {
return ''.$title.'...';
}
return $title;
}
add_action( 'the_title', 'add_suffix_to_title', 10, 1 );
Thank you in advance!
You need to add some more condition to exclude menu, like bellow:
function add_suffix_to_title($title, $id = null){
if (! is_admin()) {
if(is_singular(array('post','page')) || is_archive() || is_home()){
if(in_the_loop()){
return $title . '...';
}
}
}
return $title;
}
add_action( 'the_title', 'add_suffix_to_title', 10, 1 );

how to exclude menu items in the_title filter wordpress

I am having hard time excluding menu items from the_filter function I have to modify the post titles.
I want to modify the single post title as well as the posts in my sidebar, using in_the_loop() is working fine but it is also excluding my sidebar/archive post titles, breadcrumbs etc.
is there a way to detect menu items and exclude them? other then using in_the_loop()
here is my code:
function update_the_title( $title, $post_id ) {
if (not menu items) {
return $title + 'some string';
} else {
return $title;
}
}
add_filter( 'the_title', 'update_the_title');
Thanks
you have to try like this
Not tested
function update_the_title( $title, $post_id ) {
$menuLocations = get_nav_menu_locations();
$menuID = $menuLocations['primary']; // Get the *primary* menu ID
$primaryNav = wp_get_nav_menu_items($menuID);
if (array_search($post_id, array_column($primaryNav, 'ID'))) {
return $title + 'some string';
} else {
return $title;
}
}
add_filter( 'the_title', 'update_the_title', 10, 2);

render Visual Composer shortcodes onto page

I am trying to echo visual composer shortcodes onto a page.
I've tried both methods below, but they don't work:
functions.php:
Method 1
/*
* add shortcode file
*/
function include_file($atts) {
$a = shortcode_atts( array(
'slug' => 'NULL',
), $atts );
if($slug != 'NULL'){
ob_start();
get_template_part($a['slug']);
return ob_get_clean();
}
}
add_shortcode('include', 'include_file');
Method 2
function someshortocode_callback( $atts = array(), $content = null ) {
$output = "[vc_section full_width=\"stretch_row\" css=\".vc_custom_1499155244783{padding-top: 8vh !important;padding-bottom: 5vh !important;background-color: #f7f7f7 !important;}\"][vc_row 0=\"\"][vc_column offset=\"vc_col-lg-offset-3 vc_col-lg-6 vc_col-md-offset-3 vc_col-md-6\"][/vc_column][/vc_row][/vc_section]";
return $output;
}
add_shortcode('someshortocode', 'someshortocode_callback');
file_rendering_vc_shortcodes.php:
Method 1
<?php if ( is_plugin_active( 'js_composer/js_composer.php' ) ) {
wc_print_notice('js_composer plugin ACTIVE', 'notice');
echo do_shortcode('[include slug="vc_templates/shop-page"]');
}; ?>
Result
js_composer plugin ACTIVE
shortcode is on page with parentheses as is
Method 2
<?php $post = get_post();
if ( $post && preg_match( '/vc_row/', $post->post_content ) ) {
// Visual composer works on current page/post
wc_print_notice('VC ON', 'notice');
echo add_shortcode('someshortocode', 'someshortocode_callback');
} else {
wc_print_notice('VC OFF', 'notice');
//echo do_shortcode('[include slug="vc_templates/shop-page"]');
}; ?>
Result
VC OFF (obviously, since the vc_row in shortcode is not there)
shortcode is NOT on page
shop-page.php
<?php
/**
Template Name: Shop Page in theme
Preview Image: #
Descriptions: #
* [vc_row][vc_column][/vc_column][/vc_row]
*/
?>
[vc_section full_width="stretch_row" css=".vc_custom_1499155244783{padding-top: 8vh !important;padding-bottom: 5vh !important;background-color: #f7f7f7 !important;}"][vc_row 0=""][vc_column offset="vc_col-lg-offset-3 vc_col-lg-6 vc_col-md-offset-3 vc_col-md-6"][/vc_column][/vc_row][/vc_section]
Is it possible to render vc shortcodes on page, and if so, how is it done?
Use the:
WPBMap::addAllMappedShortcodes();
then as usual do_shortcode($content);
In short, page builder due to performance doesn't register shortcodes unless it isn't required.
If your element is registered by vc_map or vc_lean_map then no need to use add_shortcode function, you can do everything just by using WPBMap::addAllMappedShortcodes(); it is will call an shortcode class callback during the rendering process and then shortcode template.
Regarding Method 2.
You have to use do_shortcode() in your shortcode function.
function someshortocode_callback( $atts = array(), $content = null ) {
$output = '[vc_section full_width="stretch_row" css=".vc_custom_1499155244783{padding-top: 8vh !important;padding-bottom: 5vh !important;background-color: #f7f7f7 !important;}"][vc_row 0=""][vc_column offset="vc_col-lg-offset-3 vc_col-lg-6 vc_col-md-offset-3 vc_col-md-6"]column text[/vc_column][/vc_row][/vc_section]';
return do_shortcode( $output );
}
add_shortcode( 'someshortocode', 'someshortocode_callback' );
Working example on my test site: http://test.kagg.eu/46083958-2/
Page contains only [someshortocode]. Code above is added to functions.php.
In your code for Method 2 there is another error: line
echo add_shortcode('someshortocode', 'someshortocode_callback');
cannot work, as add_shortcode() returns nothing. This code should be as follows:
<?php $post = get_post();
if ( $post && preg_match( '/vc_row/', $post->post_content ) ) {
// Visual composer works on current page/post
wc_print_notice('VC ON', 'notice');
} else {
wc_print_notice('VC OFF', 'notice');
add_shortcode('someshortocode', 'someshortocode_callback');
echo do_shortcode('[someshortocode]');
}; ?>

Creating a wishlist widget

I am building quite a large website using WooCommerce and their wishlists addon. What I'd like to do is add a widget to the sidebar that shows the customers wishlists when they are logged in. There doesn't seem to be anything available at the moment so I thought I'd build it myself....
In the actual wishlists plugin files I found shortcodes-init.php which I thought would be good as a starting point to create the widget, as it should check if the person is logged in and if so, display their wishlists. All I then had to do was create the actual widget.
I created a new file that I added to the wishlists plugin folder (because I know my custom file won't get overwritten), set up the widget and added the code found in the shortcodes to display wishlists.
This is what I have:
// Creating the widget
class wl_widget extends WP_Widget {
function __construct() {
parent::__construct(
// Base ID of your widget
'wl_widget',
// Widget name will appear in UI
__('Boards Widget', 'wl_widget_domain'),
// Widget description
array( 'description' => __( 'Widget to add show customers boards', 'wl_widget_domain' ), ) );
}
// Creating widget front-end
// This is where the action happens
public function widget( $args, $instance ) {
extract( $args );
$title = apply_filters( 'widget_title', $instance['title'] );
// before and after widget arguments are defined by themes
echo $args['before_widget'];
if ( $title )
echo $before_title . $title . $after_title;
//We need to force WooCommerce to set the session cookie
if ( !is_admin() && !WC_Wishlist_Compatibility::WC()->session->has_session() ) {
WC_Wishlist_Compatibility::WC()->session->set_customer_session_cookie( true );
}
ob_start();
if (is_user_logged_in() || (WC_Wishlists_Settings::get_setting('wc_wishlist_guest_enabled', 'enabled') == 'enabled')) {
//woocommerce_wishlists_get_template('my-lists.php');
echo "loggedin";
} else {
//woocommerce_wishlists_get_template('guest-disabled.php');
echo "loggedout";
}
return ob_get_clean();
echo $args['after_widget'];
}
// Widget Backend
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'New title', 'wl_widget_domain' );
}
// Widget admin form
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<p>
This widget automatically displays boards when the person is logged in so there is no further settings needed.
</p>
<?php
}
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
} // Class btn_widget ends here
// Register and load the widget
function wl_load_widget() {
register_widget( 'wl_widget' );
}
add_action( 'widgets_init', 'wl_load_widget' );
The problem I have is that this does not see if the customer is logged in or not and actually breaks the page. This is the code that seems to be the problem.
//We need to force WooCommerce to set the session cookie
if ( !is_admin() && !WC_Wishlist_Compatibility::WC()->session->has_session() ) {
WC_Wishlist_Compatibility::WC()->session->set_customer_session_cookie( true );
}
Does anyone know how to fix this or another way to get the customers wishlists?
Thanks :)

Categories