Title: PHP Code Not Working
Last modified: August 22, 2016

---

# PHP Code Not Working

 *  Resolved [agreda](https://wordpress.org/support/users/agreda/)
 * (@agreda)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/php-code-not-working-5/)
 * 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](https://wordpress.org/support/topic/php-code-with-tags).
 * 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://wordpress.org/plugins/global-content-blocks/](https://wordpress.org/plugins/global-content-blocks/)

Viewing 5 replies - 1 through 5 (of 5 total)

 *  Thread Starter [agreda](https://wordpress.org/support/users/agreda/)
 * (@agreda)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/php-code-not-working-5/#post-5835960)
 * 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](https://wordpress.org/support/users/agreda/)
 * (@agreda)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/php-code-not-working-5/#post-5835998)
 * **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](https://wordpress.org/support/users/agreda/)
 * (@agreda)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/php-code-not-working-5/#post-5836006)
 * **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](https://wordpress.org/support/users/benz1/)
 * (@benz1)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/php-code-not-working-5/#post-5836070)
 * Glad you got it working and thanks for the donation, much appreciated 🙂
 *  [vlad13](https://wordpress.org/support/users/vlad13/)
 * (@vlad13)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/php-code-not-working-5/#post-5836385)
 * 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.

 * ![](https://s.w.org/plugins/geopattern-icon/global-content-blocks_c8cac8.svg)
 * [Global Content Blocks](https://wordpress.org/plugins/global-content-blocks/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/global-content-blocks/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/global-content-blocks/)
 * [Active Topics](https://wordpress.org/support/plugin/global-content-blocks/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/global-content-blocks/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/global-content-blocks/reviews/)

## Tags

 * [code](https://wordpress.org/support/topic-tag/code/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)

 * 5 replies
 * 3 participants
 * Last reply from: [vlad13](https://wordpress.org/support/users/vlad13/)
 * Last activity: [10 years, 7 months ago](https://wordpress.org/support/topic/php-code-not-working-5/#post-5836385)
 * Status: resolved