admin管理员组

文章数量:1122832

I use a php function to create and add share buttons to specific places through a shortcode. It work's fine, but I'm getting this error: Warning: Array to string conversion in ...142-custom-shortcodes.php on line 75

/**
 * Share buttons add_shortcode
*/

function add_social_share_buttons($content) {
    // Get the current page URL
    $url = esc_url(get_permalink());

    // Get the current page title
    $title = urlencode(html_entity_decode(get_the_title(), ENT_COMPAT, 'UTF-8'));
    
    // Social media icons URLs
    $facebook = '.png';
    $twitter = '.png';
    $linkedin = '.png';
    $pinterest = '.png';
    $instagram = '.png';
    $tiktok = '.png';

    // Create an array of social networks and their respective sharing URLs
    $social_networks = array(
        $facebook => '.php?u=' . $url,
        $twitter => '=' . $url . '&text=' . $title,
        $linkedin => '=' . $url . '&title=' . $title,
        $pinterest => '/?url=' . $url . '&description=' . $title,
    );

    // Initialize the share buttons HTML
    $share_buttons = '<div class="social-share-buttons">';

    // Loop through the social networks and generate the share buttons HTML
    foreach ($social_networks as $network => $share_url) {
        $share_buttons .= '<a href="' . $share_url . '" target="_blank" rel="noopener"><img width="36" height="36" src="' . $network . '"></a>';
    }

    // Close the share buttons HTML
    $share_buttons .= '</div>';

    // Append the share buttons HTML to the content
    $content .= $share_buttons;

    return $content;
}

// Add add shortcode
add_shortcode('compartir', 'add_social_share_buttons');

Line 75 to which the warning is pointing to is this:

$content .= $share_buttons;

Anybody had a similar problem? Thanks!

I use a php function to create and add share buttons to specific places through a shortcode. It work's fine, but I'm getting this error: Warning: Array to string conversion in ...142-custom-shortcodes.php on line 75

/**
 * Share buttons add_shortcode
*/

function add_social_share_buttons($content) {
    // Get the current page URL
    $url = esc_url(get_permalink());

    // Get the current page title
    $title = urlencode(html_entity_decode(get_the_title(), ENT_COMPAT, 'UTF-8'));
    
    // Social media icons URLs
    $facebook = 'https://fidelc30.sg-host.com/wp-content/uploads/2024/02/facebook.png';
    $twitter = 'https://fidelc30.sg-host.com/wp-content/uploads/2024/02/twitter_X.png';
    $linkedin = 'https://fidelc30.sg-host.com/wp-content/uploads/2024/02/linkedin.png';
    $pinterest = 'https://fidelc30.sg-host.com/wp-content/uploads/2024/02/pinterest.png';
    $instagram = 'https://fidelc30.sg-host.com/wp-content/uploads/2024/02/instagram.png';
    $tiktok = 'https://fidelc30.sg-host.com/wp-content/uploads/2024/02/tiktok.png';

    // Create an array of social networks and their respective sharing URLs
    $social_networks = array(
        $facebook => 'https://www.facebook.com/sharer/sharer.php?u=' . $url,
        $twitter => 'https://twitter.com/intent/tweet?url=' . $url . '&text=' . $title,
        $linkedin => 'https://www.linkedin.com/shareArticle?url=' . $url . '&title=' . $title,
        $pinterest => 'https://pinterest.com/pin/create/button/?url=' . $url . '&description=' . $title,
    );

    // Initialize the share buttons HTML
    $share_buttons = '<div class="social-share-buttons">';

    // Loop through the social networks and generate the share buttons HTML
    foreach ($social_networks as $network => $share_url) {
        $share_buttons .= '<a href="' . $share_url . '" target="_blank" rel="noopener"><img width="36" height="36" src="' . $network . '"></a>';
    }

    // Close the share buttons HTML
    $share_buttons .= '</div>';

    // Append the share buttons HTML to the content
    $content .= $share_buttons;

    return $content;
}

// Add add shortcode
add_shortcode('compartir', 'add_social_share_buttons');

Line 75 to which the warning is pointing to is this:

$content .= $share_buttons;

Anybody had a similar problem? Thanks!

Share Improve this question asked Apr 17, 2024 at 19:50 GKaplanGKaplan 33 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

This is happening because in the function the first parameter is named $content and you use it as HTML content:

function add_social_share_buttons($content) {
....
    // Append the share buttons HTML to the content
    $content .= $share_buttons;

    return $content;
}

But the first parameter of a shortcode callback is not a string of content, it's an array of attributes:

https://developer.wordpress.org/reference/functions/add_shortcode/

It should instead look more like this:

function add_social_share_buttons( $attributes, string $content = '', string $shortcode ): string {

Where:

[$shortcode $attributes]$content[/$shortcode]

Note the addition of PHP type hinting, and note that $attributes is sometimes an array, and sometimes it's an empty string, be sure to set default values using the shortcode_atts function:

https://developer.wordpress.org/reference/functions/shortcode_atts/

Also consider building a custom block for this instead using a PHP rendered block rather than JS for a similar experience.

本文标签: I have a php function with a shortcode to add share buttonsbut I39m having an error