WordPress短代码功能有什么代码错误?

时间:2015-10-24 06:40:38

标签: php wordpress shortcode

我对此代码有疑问。 有些人破坏了我的WordPress网站,我不知道哪个,我不知道php或编码那么多。

function check_member( $atts, $content = null )
    {
        if ( is_user_logged_in() ) :
            return $content;
        else :
            $return .= '<img class="background-img" src="website.com/image.jpg" alt="" width="850" height="478" style="position: absolute; top: 0; left: 0;" />';
            $return .= '<img class="logo-img" src="website.com/image.jpg" alt="" width="188" height="50" style="position: absolute; display: block; left: 0; right: 0; bottom: 50%; margin: auto;" />';
            $return .= '<h2 style="position: absolute; text-transform: uppercase; margin: 0; font-size: 1.5rem; color: white; display: block; left: 0; right: 0; bottom: 10%; margin: auto; padding: 50px;">NOT OKAY</h2>';
            $return .= '<p style="position: absolute; margin: 0; font-size: 1.2rem; color: white; display: block; left: 0; right: 0; bottom: 0%; margin: auto; padding: 50px;">SORRY, YOU ARE NOT MEMBER</p>';
        endif;
    }

1 个答案:

答案 0 :(得分:0)

如果这会在您的页面上发出警告,可能是因为您的第一行。由于您未在任何地方声明$return,因此您无法使用.=,而是使用=

$return = '<img class="background-img" src="website.com/image.jpg" alt="" width="850" height="478" style="position: absolute; top: 0; left: 0;" />';

其次,你不需要像这样连接,每一行都不必分成它自己的语句:

$return = '<img class="background-img" src="website.com/image.jpg" alt="" width="850" height="478" style="position: absolute; top: 0; left: 0;" />
<img class="logo-img" src="website.com/image.jpg" alt="" width="188" height="50" style="position: absolute; display: block; left: 0; right: 0; bottom: 50%; margin: auto;" />
<h2 style="position: absolute; text-transform: uppercase; margin: 0; font-size: 1.5rem; color: white; display: block; left: 0; right: 0; bottom: 10%; margin: auto; padding: 50px;">NOT OKAY</h2>
<p style="position: absolute; margin: 0; font-size: 1.2rem; color: white; display: block; left: 0; right: 0; bottom: 0%; margin: auto; padding: 50px;">SORRY, YOU ARE NOT MEMBER</p>';

第三,你没有返回这个变量,所以当它到达脚本的后半部分时什么都不会发生。你需要退货:

$return = '<img class="background-img" src="website.com/image.jpg" alt="" width="850" height="478" style="position: absolute; top: 0; left: 0;" />
<img class="logo-img" src="website.com/image.jpg" alt="" width="188" height="50" style="position: absolute; display: block; left: 0; right: 0; bottom: 50%; margin: auto;" />
<h2 style="position: absolute; text-transform: uppercase; margin: 0; font-size: 1.5rem; color: white; display: block; left: 0; right: 0; bottom: 10%; margin: auto; padding: 50px;">NOT OKAY</h2>
<p style="position: absolute; margin: 0; font-size: 1.2rem; color: white; display: block; left: 0; right: 0; bottom: 0%; margin: auto; padding: 50px;">SORRY, YOU ARE NOT MEMBER</p>';

// This will send it back to use via echo
return $return;

第四,你的$atts没有被使用,没有意义。它可能也会破坏你的代码。

NON-SHORTCODE

如果你没有做[shortcode],这可能就是你要找的东西:

function check_member($content = null )
    {
        if (is_user_logged_in())
            return $content;

        ob_start();
        ?>
        <img class="background-img" src="website.com/image.jpg" alt="" width="850" height="478" style="position: absolute; top: 0; left: 0;" />
        <img class="logo-img" src="website.com/image.jpg" alt="" width="188" height="50" style="position: absolute; display: block; left: 0; right: 0; bottom: 50%; margin: auto;" />
        <h2 style="position: absolute; text-transform: uppercase; margin: 0; font-size: 1.5rem; color: white; display: block; left: 0; right: 0; bottom: 10%; margin: auto; padding: 50px;">NOT OKAY</h2>
        <p style="position: absolute; margin: 0; font-size: 1.2rem; color: white; display: block; left: 0; right: 0; bottom: 0%; margin: auto; padding: 50px;">SORRY, YOU ARE NOT MEMBER</p>
        <?php
        $content    =   ob_get_contents();
        ob_end_clean();

        return $content;
    }

使用:

$whatever = 'Some sort of text that will be written if logged in.';
echo check_member($whatever);

SHORTCODE

如果您想要[shortcode],您可能会希望这样做:

function check_member($attr)
    {
        $settings['content']    =   false;
        $a                      =   shortcode_atts($settings,$attr,'checkmember');

        if (is_user_logged_in())
            return $a['content'];

        ob_start();
        ?>
        <img class="background-img" src="website.com/image.jpg" alt="" width="850" height="478" style="position: absolute; top: 0; left: 0;" />
        <img class="logo-img" src="website.com/image.jpg" alt="" width="188" height="50" style="position: absolute; display: block; left: 0; right: 0; bottom: 50%; margin: auto;" />
        <h2 style="position: absolute; text-transform: uppercase; margin: 0; font-size: 1.5rem; color: white; display: block; left: 0; right: 0; bottom: 10%; margin: auto; padding: 50px;">NOT OKAY</h2>
        <p style="position: absolute; margin: 0; font-size: 1.2rem; color: white; display: block; left: 0; right: 0; bottom: 0%; margin: auto; padding: 50px;">SORRY, YOU ARE NOT MEMBER</p>
        <?php
        $content    =   ob_get_contents();
        ob_end_clean();
        return $content;
    }

add_shortcode('checkmember','check_member');