• Resolved agreda

    (@agreda)


    First off, thanks for all your hard work on this plugin! I’ve got it working great with some random image Javascript goodness. But I’m having trouble getting the following PHP code to render…

    <?php if (is_user_logged_in()): ?>
    <?php require('/home/userx/public_html/chatroom/chat.php'); ?>
    <?php else: ?>
    <p>You must be logged in to join the chat.</p>
    <?php endif; ?>

    I have tried removing all the opening <?php and closing ?> tags as directed. I have also tried including the entire code, wrapping it in close/open tags as suggested here.

    I hard-coded this into a page template and confirmed that the chat room renders and functions, but I’d much rather use a shortcode to insert it in a page. What might I be doing wrong?

    Does each function need to be in a separate Content Block? And if so, how do I handle the if/else statement and content?

    Thanks again for any direction anyone can provide!

    https://www.ads-software.com/plugins/global-content-blocks/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter agreda

    (@agreda)

    PS: I did get a simple PHP echo statement to work (without the open/close tags) but I can’t seem to figure out what’s going on with the code above. Nothing renders on the page, and no errors… :-\

    Thread Starter agreda

    (@agreda)

    FYI: The chat room renders and works fine when inserted via shortcode with block containing the following content:

    require('/home/userx/public_html/chatroom/chat.php');

    Any direction for including the if/else statement to only show it for logged in members is greatly appreciated.

    Thanks again!

    Thread Starter agreda

    (@agreda)

    Fixed it! Sometimes you just need to work through these things out loud, even if no one is listening. ??

    For anyone curious, here is the conditional PHP code I used in the Content Block to render a chat room, only for logged in users…

    if (is_user_logged_in()) {
        require('/home/userx/public_html/chatroom/chat.php');
    } else {
         echo "<p>You must be logged in to join the chat.</p>";
    }
    Plugin Author benz1

    (@benz1)

    Glad you got it working and thanks for the donation, much appreciated ??

    vlad13

    (@vlad13)

    Hello there,
    Can you implement a change that would allow having php code inside HTML fragments?

    All you need to do is replace the following block of code inside the “gcb” function:

    if($entry['type']!="php") {
    	return apply_filters('gcb_block_output', do_shortcode($content));//make sure we also run the shortcodes in here
    }
    else
    {
    	//execute the php code
    	ob_start();
    	$result = eval(" ".$content);
    	$output = ob_get_contents();
    	ob_end_clean();
    	return apply_filters('gcb_block_output', do_shortcode($output . $result));//run the shortcodes as well
    }		}

    Replace with:

    if($entry['type'] == "php") {
        //execute the php code
        ob_start();
        $result = eval(" ".$content);
        $output = ob_get_contents();
        ob_end_clean();
        return apply_filters('gcb_block_output', do_shortcode($output . $result));//run the shortcodes as well
    }
    elseif($entry['type'] == "html") {   // alloyphoto: enable PHP code in < ?php ... ? > tags inside blocks
        ob_start();
        eval("?>$content<?php ");
        $output = ob_get_contents();
        ob_end_clean();
        return apply_filters('gcb_block_output', do_shortcode($output));//run the shortcodes as well
    }
    else
    {
        return apply_filters('gcb_block_output', do_shortcode($content));//make sure we also run the shortcodes in here
    }

    Thank you!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘PHP Code Not Working’ is closed to new replies.