0
votes

Has anyone encountered this problem before? Directly calling the shortcode from WordPress and calling the shortcode via functions.php yields different results.

Refer to this img:

Image 1 has decent design since it was invoked directly from WordPress UI while Image 2 somehow messed up the design because it was invoked from php (functions.php).

Additional info:

  • Current theme is DIVI
  • Shortcode/Plugin used is TableMaster

To add further information, I have this line of shortcode in WordPress (as seen on image 2).

[get_blogs_sc]

And on my functions.php, I have this function,

function GetActiveBlogs($i)
{ 

    $actual_link = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

    if (preg_match('/sites/',$actual_link))
    {

        $user_id = get_current_user_id();
        if ($user_id > 0) 
        {

            echo do_shortcode('[tablemaster buttons="true" datatables="true" class="black-header-gray-alternate-rows" sql="some select where user_id = '.user_id.'"]', true);
        }
    }
}


add_shortcode('get_blogs_sc', 'GetActiveBlogs');

But when I use the shortcode directly from WordPress:

[tablemaster buttons="true" datatables="true" class="black-header-gray-alternate-rows" sql="some select where user_id = 14]

the display on my page looks good (as seen on image 1)

My objective on putting the shortcode in php-layer is for me to be able to capture the logged-in user from WordPress.

Thanks.

1

1 Answers

0
votes

it's hard to diagnose what the issue is only from images ! without what the code really says. but let's know what difference between do_shortcode and add_shortcode

do_shortcode( string $content, bool $ignore_html = false )

Search content for shortcodes and filter shortcodes through their hooks. it return (string) Content with shortcodes filtered out.

// Use shortcode in a PHP file (outside the post editor).
echo do_shortcode( '' );

for more information check link

while add_shortcode Adds a hook for a shortcode tag.

<?php add_shortcode( $tag , $func ); ?>

$tag (string) (required) Shortcode tag to be searched in post content Default: None $func (callable) (required) Hook to run when shortcode is found Default: None

it return nothing but when you write shortcode tag in post area it apply the function of shortcode for more information check Link