Calling Yoast SEO title in widget not work - php

I just install Wordpress SEO plugin by Yoast, test the title re-write function and it works in my title bar (single post). However when I try to call the title in widget, I just get error message
Warning: Missing argument 1 for kpndg()...
I insert this code in theme functions.php
add_filter( 'wpseo_title', 'kpndg' );
function kpndg( $title ) {
return $title;
}
Then call it in my widget:
$out.='<div class="trending-bar add-active bar-' . $i . '">';
$out.='<a class="trending-link" href="'.get_permalink().'"> </a>';
$out.='<div class="title">'.kpndg().'</div>';
$out.='<div class="trending-color-wrapper">';
$out.='<div class="trending-color-layer"></div>';
$out.='<div class="trending-color"></div>';
$out.='<div class="trending-meta">' . $meta . '</div>';
$out.='</div>';
$out.='</div>';
I can't figure out what is the problem in that code.
FYI: I use Engine theme by Industrial Themes

This line: $out.='<div class="title">'.kpndg().'</div>';
You need to specify an argument... Or, alternatively, set a default value for the $title argument:
add_filter( 'wpseo_title', 'kpndg' );
function kpndg( $title = 'foo' ) {
return $title;
}
Edit
Are you sure you didn't mean to use get_post_meta(get_the_ID(), '_yoast_wpseo_title', true); in place of kpndg()?
Edit #2
Instead of adding a new filter for wpseo_title, just create a "helper function":
function kpndg() {
return get_post_meta(get_the_ID(), '_yoast_wpseo_title', true) ?: get_the_title();
}
Then, within the loop, you can call echo kpndg()

Related

Customizing the Parents functions.php File in Child

I'm customizing my theme right now. The Standard is, that the logged-in Users Avatar is shown in the Menu bar but I want to display a simple Icon there. I already wright the Code and everything works fine but the Code is located in the mains functions.php File. So in order to make the Site upgradeable, I need to embed the code in the child themes function.php. I found no help there so maybe here is someone who could help me!
Thanks a lot
Below is the Code which I need to embed in the functions.php File of my Child Theme:
add_action( 'init', 'gt3_get_page_id_from_menu' );
function gt3_user_avatar_out($avatar_size=80,$show_email=false){
$user_id = get_current_user_id();
$current_user = get_user_by( 'id', $user_id );
echo "<i class='gt3_login_icon gt3_login_icon--avatar'>";
echo '<i class="fa fa-user"></i>';
echo "</i>";
if ($show_email) {
echo "<span class='gt3_login__user_email'>".esc_html( $current_user->user_email )."</span>";
}
}
If a function is declared in the parent's functions.php, declaring a function with the same name in the child theme will cause a fatal error in the child theme itself. I'd suggest doing something like this instead:
parent function .php
// This can become a filter if you'll need to manipulate the current user
function gt3_get_current_user() {
return get_user_by( 'id', get_current_user_id() );
}
add_filter( 'gt3/current_user_avatar', function( string $html = '', bool $show_email = false ) {
$html .= '<i class="gt3_login_icon gt3_login_icon--avatar"><i class="fa fa-user"></i></i>';
if( $show_email ) {
$html .= sprintf( '<span class="gt3_login__user_email">%s</span>', esc_html( gt3_get_current_user()->user_email ) );
}
return $html;
}, 10, 2 );
child theme's function .php
add_filter( 'gt3/current_user_avatar', function( string $html = '', bool $show_email = false ) {
// $html here is the one that returned from the parent's theme. You can manipulate it or replace the whole output inside here.
return $html;
}, 10, 2 );
Example usages:
echo apply_filters( 'gt3/current_user_avatar', '', false ); // This will echo the icon without the email
echo apply_filters( 'gt3/current_user_avatar', '', true ); // This will echo the icon with the email
echo apply_filters( 'gt3/current_user_avatar', '<p>Append</p>', false ); // This will echo the icon without the email and the '<p>Append</p>' html string before everything else
The result will vary depending on where you use this filter. If you are in the child theme and you altered the html adding the said filter, the result will be customized for that theme. Remember that you can use the use statement to bring in external variables ina closure. For example:
$user_name = 'Mike';
add_filter( 'gt3/current_user_avatar', function( string $html = '', bool $show_email = false ) use ( $user_name ) {
$html .= sprintf( '<p>Hello, %s!</p>', $user_name );
return $html;
}, 10, 2 );
// This will cheer the user, and it will be place at the end of the snippet.
your code is not overwritten by adding a new functions.php file in the same folder, but do not copy/paste the code from the parent theme’s file, because it needs to remain separate from any modifications you make to the child theme. Instead, create a blank file or add any new .php functions required for your child theme

How to Use Title() Filter Hook to Change Page Titles, but Not Menu Titles

I want to use a filter hook to add an image before titles of my pages, but it also adds them to my menu titles. How can I get around this?
function scissorTitle ($title)
{
$scissortitle .= "<img src='*image_link*'>" . $title;
return $scissortitle;
}
add_filter( 'title', 'scissorTitle', 10);
The filter title has two parameters $title and $id you can use $id to change the only specific title of the page Check docs. Your full code should look like this:
function scissorTitle ($title, $id) {
global $post;
if (is_singular() && $post->ID == $id) {
$title = '<img src="image_link">' . $title;
}
return $title;
}
add_filter( 'the_title', 'scissorTitle', 10, 2);

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]');

Use two shortcodes in 1

I was wondering if I can use two short codes in 1. I know you can replace variables in shortcodes but what I'm looking for is to use a shortcode say [apple] which will provide a link to the link I give in the shortcode but then if I want an icon next to that link be able to use [apple icon] which than provide a link with the icon to the left of it.
add_shortcode('apple', 'apple');
function apple()
{
return '<a href="http://example.com/apple>Apple</a>';
}
So is it possible to add icon to where if we add icon to the short code it will also return an image too which I would specify in the shortcode.
You will want to use attributes.
add_shortcode( 'apple', 'apple' );
function apple( $atts, $content = null )
{
extract( shortcode_atts( array( 'icon' => 'false'), $atts ) );
if($icon == 'true'){
$output = '<img src="apple.png" />';
}
$output .= 'Apple';
return $output;
}
You would then call it like
[apple icon="true"]
Here is the documentation https://codex.wordpress.org/Function_Reference/add_shortcode
If I understand your question correctly you shouldn't need to create a shortcode in a shortcode. The shortcode is already executing within your plugin/functions so you should be able to simply invoke your function (or the icon function) directly within your shortcode handler function. No need to run it through shortcodes again.
If it's not your own shortcode you are looking to call you can call do_shortcode
http://codex.wordpress.org/Function_Reference/do_shortcode
To do what you want, it's not necessary to have two shortcodes, just make your shortcode accept arguments:
add_shortcode('apple', 'apple');
function apple($args) {
$default = array('icon' => '');
$args = wp_parse_args($args, $default);
$content = '';
if ($args['icon']) {
$content.= '<img src="icon.png">';
}
$content.= '<a href="http://example.com/apple>Apple</a>';
return $content;
}
To use this, you would then enter your shortcode as follows:
[apple icon="yes"]
If it's simply a "yes/no" for the icon.
Or, you could make it so it loads the icon dynamically based on what you set icon equal to, which would require some modifications to the function:
function apple($args) {
$default = array('icon' => '');
$args = wp_parse_args($args, $default);
$content = '';
if ($args['icon']) {
$content.= '<img src="' . $args['icon'] . '">';
}
$content.= '<a href="http://example.com/apple>Apple</a>';
return $content;
}
And your shortcode would then look like so:
[apple icon="my_icon.png"]
(Note, you'd probably need to pass in a fully qualified domain name, like so: [apple icon="http://example.com/images/my_icon.png"])

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