posts - Defined function isn't showed on page

admin2025-06-02  0

I've defined a function in function.php and then I tried to call it but it's not visible when I call it.

This is the function

function social_sharing_buttons($content) {
    global $post;
    if(!is_page()){

        // Get current page URL 
        $URL = urlencode(get_permalink());

        // Get current page title
        $Title = htmlspecialchars(urlencode(html_entity_decode(get_the_title(), ENT_COMPAT, 'UTF-8')), ENT_COMPAT, 'UTF-8');

        $facebookURL = '.php?u='.$URL;

        $variable .= '<div class="social">';;
        $variable .= '<a class="link facebook" href="'.$facebookURL.'" target="_blank">Facebook</a>';
        $variable .= '</div>';

        return $variable.$content;
    }else{
        return $variable.$content;
    }
};
add_filter( 'the_content', 'social_sharing_buttons');

The function simply add a facebook share button on the Single post page.

I've tried to call it in single.php like this:

echo '<div class="post_cont_info entry-meta">'; 

    social_sharing_buttons('post');

echo '</div>';

I've defined a function in function.php and then I tried to call it but it's not visible when I call it.

This is the function

function social_sharing_buttons($content) {
    global $post;
    if(!is_page()){

        // Get current page URL 
        $URL = urlencode(get_permalink());

        // Get current page title
        $Title = htmlspecialchars(urlencode(html_entity_decode(get_the_title(), ENT_COMPAT, 'UTF-8')), ENT_COMPAT, 'UTF-8');

        $facebookURL = 'https://www.facebook/sharer/sharer.php?u='.$URL;

        $variable .= '<div class="social">';;
        $variable .= '<a class="link facebook" href="'.$facebookURL.'" target="_blank">Facebook</a>';
        $variable .= '</div>';

        return $variable.$content;
    }else{
        return $variable.$content;
    }
};
add_filter( 'the_content', 'social_sharing_buttons');

The function simply add a facebook share button on the Single post page.

I've tried to call it in single.php like this:

echo '<div class="post_cont_info entry-meta">'; 

    social_sharing_buttons('post');

echo '</div>';
Share Improve this question asked Mar 7, 2019 at 14:05 S.I.S.I. 1251 silver badge8 bronze badges 2
  • echo social_sharing_buttons('post'); – Max Yudin Commented Mar 7, 2019 at 14:12
  • you mentioned function.php are you sure that's the filename, and not functions.php? – Tom J Nowell Commented Mar 7, 2019 at 14:37
Add a comment  | 

1 Answer 1

Reset to default 1

There's no relationship between how the function is written, and how you're attempting to use it.

The first problem is that your function returns a value, rather than echoing it. So if you wanted to output the value you need to use echo:

echo social_sharing_buttons('post');

But the usage of 'post' doesn't make sense here. Your code will just output this:

<div class="post_cont_info entry-meta">
    <div class="social">
        <a class="link facebook" href="(etc. etc.)" target="_blank">Facebook</a>
    </div>
    post
</div>

Notice the post hanging out there at the end.

This is because your social_sharing_buttons() function is written to filter the_content() so that your custom link appears at the top of the post content. This means that it accepts the full content of the post as an argument — not a post type — and then returns the value back to WordPress to be output by the the_content() function. So you can't use it to output the share link in an arbitrary template.

If you want to write the function so that it only outputs this link, you need to rewrite it to look like this:

function social_sharing_buttons( $after = '' ) {
    $variable = '';

    if ( ! is_page() ) {
        $url = urlencode( get_permalink() );

        $facebookURL = 'https://www.facebook/sharer/sharer.php?u=' . $url;

        $variable .= '<div class="social">';;
        $variable .= '<a class="link facebook" href="'.$facebookURL.'" target="_blank">Facebook</a>';
        $variable .= '</div>';
    }

    return $variable . $after;
}

Note the following changes:

  • The $content parameter has been renamed something generic that makes sense even if it's not used as a filter, and has been given a default value so that you can echo it without passing an argument.
  • global $post has been removed, since it served no purpose.
  • $Title has been removed, because sharer.php does not support any other parameters than the URL, so it served no purpose.
  • $variable has been removed to avoid unnecessary repetition in an else condition.

Now you can output it like this:

echo social_sharing_buttons();

But it will also work as a filter for the_content():

add_filter( 'the_content', 'social_sharing_buttons' );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748833208a314102.html

最新回复(0)