Wordpress insert shortcode into menu item - php

I have such a problem. In functions.php I have such code:
function Svg_Path($attr) {
$a = shortcode_atts(array(
'path' => 'Some text'
), $attr);
$svg = '<svg class="menu-item-icon"><use xlink:href=' . get_template_directory_uri(). '/img/svg/sprite.svg' . $a["path"] . '></use></svg>';
return $svg;
}
add_shortcode( 'SvgPath', 'Svg_Path' );
Normally, I can use in pageBuilder this
<div>[SvgPath path='#logo__skype']</div>
And the result will be the displayed Skype Icon. But when I try to insert this shortcode into Appearence>Menu>Link text I get the shortcode as plain text
Where do I try to input my shortcode
So can you help me wwith this, so I can recieve skype logo from svg sprite in menu item

Try the following code. The code will parse your shortcode and show you the shortcode content. It'll only work on frontend.
add_filter( 'the_title', function( $title, $item_id ) {
if ( 'nav_menu_item' === get_post_type( $item_id ) ) {
return do_shortcode( $title );
}
return $title;
}, 10, 2 );

Can you not use the updated FontAwesome to create this and add this into your menu instead?
https://fontawesome.com/icons/skype?style=brands may help you out.

Related

Is this way using a Custom Field to change Add To Card Text in WooCommerce good?

I want to be able to display a specific Add To Card Text for each single product, if set.
There are some plugins out there, but they "just" cover Add To Card Text for categories or product types. That's why I tried this way using a Custom Field.
So I wrote this code directly in a template file. This code works, but is it good, safe, at the right place?
// file: /woocommerce-templates/single-product/add-to-cart/simple.php
<button "..." >
<?php
$text = get_post_meta($product->get_id(), 'add_to_cart_text', true);
if ($text) {
echo esc_html($text);
} else {
echo esc_html($product->single_add_to_cart_text()); // this line is default
}
?>
</button>
I would use this instead:
<?php
add_filter( 'woocommerce_product_add_to_cart_text', 'custom_add_to_cart_button_text', 20, 2 );
function custom_add_to_cart_button_text( $button_text, $product ) {
$custom_button_text = get_post_meta( $product->get_id(), 'add_to_cart_text', true );
// If there is custom text set for this product, then display it instead of the default text.
if ( ! empty( $custom_button_text ) ) {
$button_text = __( $custom_button_text, 'text-domain' );
}
return $button_text;
}
Paste it in your functions.php

Wordpress shortcodes bug in custom theme

I've created a theme from scratch and I have issues creating shortcodes. I have the following code:
functions.php
function caption_shortcode( $atts, $content = null ) {
return '<span class="caption">' . $content . '</span>';
}
add_shortcode( 'caption', 'caption_shortcode' );
in the WP Admin page editor:
[caption]My Caption[/caption]
on the page template page:
echo do_shortcode('[caption]');
The shortcode seems to be somehow working as it returns the HTML but not the $content.
My problem is that I can't seem to get my hand on the $content and display it using the shortcode. Any idea why this is happening?
P.S. I don't want to use the_content() function to display all the content, I want to use the shortcodes to divide the content the user adds in several pop-ups and child sections of the page.
Thanks!
Make sure you user shotcode same page
// [baztag]content[/baztag]
function baztag_func( $atts, $content = '' ) {
return $content;
}
add_shortcode( 'baztag', 'baztag_func' );
echo do_shortcode('[baztag]');

Custom button on WP Tiny MCE Posts Editor for Shortcodes

Trying to create a special button on the Tiny MCE editor on the Post Editor of Wordpress. I want to create a button for my created shortcode tooltips.
Check out my desired output:
I have the following code for my Tooltip shortcode:
//Text tooltip
function tooltip($atts, $content = ''){
$atts = shortcode_atts(
array(
'class' => '',
'title' => ''
), $atts);
$html = '<a class="' . $atts['class'] .'" title="'. $atts['title']. '" href="#">' . $content . ' <span>' .$atts['title']. '</span> </a>';
return $html;
}
add_shortcode('tooltip', 'tooltip');
Now when you execute this you will use the following codes for the shortcode:
[tooltips class="top_tooltip" title="Your Tooltip here"] Text here [/tooltip]
What I have done is that created some function to DISPLAY MY CREATED CUSTOM BUTTON TOOLTIP SHORTCODE on the functions.php file on my theme using the following codes.
//Create Tiny MCE buttons
function mce_tooltip($button) {
$buttons[] = 'superscript';
return $button;
}
add_filter('mce_buttons_2', 'mce_tooltip');
/*
* Callback function to filter the MCE settings
*/
function mce_tooltip( $init_array ) {
// Define the style_formats array
$style_formats = array(
// Each array child is a format with it's own settings
array(
'class' => '',
'title' => ''
);
// Insert the array, JSON ENCODED, into 'style_formats'
$init_array['style_formats'] = json_encode( $style_formats );
return $init_array;
}
// Attach callback to 'tiny_mce_before_init'
add_filter( 'tiny_mce_before_init', 'mce_tooltip' );
I tried the code but it won't show up my CUSTOM button for my shortcode on the TINY MCE Editor on Wordpress. Any idea how to do it better?
Check your code. I guess there is invalid return value
function mce_tooltip($button) {
$buttons[] = 'superscript';
return $button;
}
For TinyMCE Advanced Version: 4.1.1 follow this steps:
In /wp-content/plugins/tinymce-advanced/tinymce-advanced.php (function get_all_buttons()) add $buttons[] = 'yourButton'
http://prntscr.com/5ywb4p
In /wp-includes/js/tinymce/tinymce.min.js after some button add your own
e.addButton('yourButton',{title:'yourButton',text:"yourButton",'class' : 'yourButton-btn', onclick : function() { e.focus(); e.selection.setContent('<YourTag>' + e.selection.getContent() + '</YourTag>'); } })
http://prntscr.com/5ywc48
Then go into tinymce settings in wp-admin and add your new button to editor.
http://www.gavick.com/blog/adding-your-own-buttons-in-tinymce-4-editor/
Try this tutorial, it works. It teaches how to add a custom button to the new tinymce 4 and how to use it for your own shortcodes

How to add a sidebar to WordPress menus

I need to get a widget inside a menu and I'm trying:
add_filter('wp_nav_menu_items', 'add_widget_to_menu', 10, 2);
function add_widget_to_menu($items, $args) {
if ($args->theme_location == 'mobile_menu')
return $items .
'<div id="newsletter-mobile">' .
dynamic_sidebar('sidebar-mobile' ) .
'</div>';
return $items;
}
What's wrong?
This hook is used to add a sidebar menu in WordPress:
add_action("admin_menu", "sidebar_menu_for_registration");
sidebar_menu_for_registration is a function.
This is the syntax to create a sidebar menu:
add_menu_page(string $page_title, string $menu_title, string $capability, string $menu_slug, callable $function = '', string $icon_url = '', int $position = null)
You can create a sidebar using the following
register_sidebar(array(
'name'=>'Widget_Name',
'id'=>'widget_id',
'before_widget' =>'<div class="">',
'after_widget' =>'</div>'
));
To call this in your template, you can use the following code:
if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('widget_id')) :
endif;
Refer here for more details.
You will get 1 as output adding widget area to menu like that. The reason is that wp_nav_menu echoes the items. To make it work you should convert the contents of the widget area to string. I managed to solve the problem using buffering. Here is a small code sample that works for me:
function add_extra_item_to_nav_menu( $items, $args ) {
ob_start();
dynamic_sidebar( 'header-widget-area' );
$menu_widget_area = ob_get_clean();
$items .= $menu_widget_area;
return $items;
}
add_filter( 'wp_nav_menu_items', 'add_extra_item_to_nav_menu', 10, 2 );

Why does the_title() filter is also applied in menu title?

I have created below function to hide page title. But when I execute this code, it also hides the menu name.
function wsits_post_page_title( $title ) {
if( is_admin())
return $title;
$selected_type = get_option('wsits_page_show_hide');
if(!is_array($selected_type)) return $title;
if ( ( in_array(get_post_type(), $selected_type ) ) && get_option('wsits_page_show_hide') )
{
$title = '';
}
return $title;
}
add_filter( 'the_title', array($this, 'wsits_post_page_title') );
Nikola is correct:
Because menu items also have titles and they need to be filtered :).
To make this only call in the posts, and not in menus, you can add a check for in_the_loop() - if it is true, you're in a post.
So change the first line in the function to:
if( is_admin() || !in_the_loop() )
and all should be well.
It's a bit of a hack but you can solve this by adding your action to loop_start.
function make_custom_title( $title, $id ) {
// Your Code Here
}
function set_custom_title() {
add_filter( 'the_title', 'make_custom_title', 10, 2 );
}
add_action( 'loop_start', 'set_custom_title' );
By embedding the_title filter inside of a loop_start action, we avoid overwriting the menu title attributes.
You can do something like that :
In your function.php :
add_filter( 'the_title', 'ze_title');
function ze_title($a) {
global $dontTouch;
if(!$dontTouch && !is_admin())
$a = someChange($a);
return $a;
}
In your template :
$dontTouch = 1;
wp_nav_menu( array('menu' => 'MyMenu') );
$dontTouch = 0;
Posting this answer because it was the search result I ended up clicking on while searching about targeting the filter hook the_title while ignoring the filter effect for navigation items.
I was working on a section in a theme which I wanted to add buttons to the page title within the heading one tag.
It looked similar to this:
<?php echo '<h1>' . apply_filters( 'the_title', $post->post_title ) . '</h1>'.PHP_EOL; ?>
I was then "hooking in" like this:
add_filter( 'the_title', 'my_callback_function' );
However, the above targets literally everything which calls the_title filter hook, and this includes navigation items.
I changed the filter hook definition like this:
<?php echo '<h1>' . apply_filters( 'the_title', $post->post_title, $post->ID, true ) . '</h1>'.PHP_EOL; ?>
Pretty much every call to the_title filter passes parameter 1 as the $post->post_title and parameter 2 as the $post->ID. Search the WordPress core code for apply_filters( 'the_title'* and you'll see for yourself.
So I decided to add a third parameter for situations where I want to target specific items which call the_title filter. This way, I can still receive the benefit of all callbacks which apply to the_title filter hook by default, while also having the ability to semi-uniquely target items that use the_title filter hook with the third parameter.
It's a simple boolean parameter:
/**
* #param String $title
* #param Int $object_id
* #param bool $theme
*
* #return mixed
*/
function filter_the_title( String $title = null, Int $object_id = null, Bool $theme = false ) {
if( ! $object_id ){
return $title;
}
if( ! $theme ){
return $title;
}
// your code here...
return $title;
}
add_filter( 'the_title', 'filter_the_title', 10, 3 );
Label the variables however you want. This is what worked for me, and it does exactly what I need it to do. This answer may not be 100% relevant to the question asked, but this is where I arrived while searching to solve this problem. Hope this helps someone in a similar situation.
The global $dontTouch; solution didn't work for me for some reason. So I simply removed the filter around the menu thus in header.php:
remove_filter( 'the_title', 'change_title' );
get_template_part( 'template-parts/navigation/navigation', 'top' );
add_filter( 'the_title', 'change_title' );
And all is well.
I think you're looking for this:
function change_title($title) {
if( in_the_loop() && !is_archive() ) { // This will skip the menu items and the archive titles
return $new_title;
}
return $title;
}
add_filter('the_title', array($this, 'change_title'), 10, 2);

Categories