Using wordpress shortcodes to render PHP -


i'm trying create shortcode render custom php pages. i'm getting odd behaviour i'm not getting if used wordpress template.

for example. if create template code:

<article id="post-<?php the_id(); ?>" <?php post_class(); ?> <?php generate_article_schema( 'blogposting' ); ?>> <div class="inside-article"> <?php $content = apply_filters( 'the_content', get_the_content() );      global $post;     $post_info = get_post($post->id);     $content =  $post_info->post_content;      $ind = strrpos($content, "/");      $page = substr($content, $ind+1);     $path = substr($content, 0, $ind+1);     $olddir = getcwd();    chdir($_server["document_root"].$path);    include($page);    chdir($olddir);  ?>   </div><!-- .inside-article --> </article><!-- #post-## --> 

then in wordpress page enter location of file, e.g. /contact/contact_form.php. contact_form.php page renders on screen within wordpress theme.

now, discovered shortcodes, , thought these better way of rendering php on page. instead of having content of page /contact/contact_form.php have [custom_php filepath="/contact/contact_form.php"]

this i'm getting problems. contact_form.php seems have many require_once() functions on place, yet variables wtihin them don't seem exist, though first method (above) works fine.

for shortcode code have:

function subscribe_custom_php_shortcode($atts, $content = null) { $atts = shortcode_atts(     array(         'filepath' => '/index.php'     ), $atts);       $ind = strrpos($atts["file"], "/");     $page = substr($atts["file"], $ind+1);     $path = substr($atts["file"], 0, $ind+1);     $olddir = getcwd();     chdir($_server["document_root"].$path);      ob_start();     include($page);      $content = ob_get_clean();           chdir($olddir);     return $content; } add_shortcode('custom_php', 'subscribe_custom_php_shortcode'); 

as can see, code similar except added ob_start() , ob_get_clean() results of php based on someones comments. removing these still doesn't seem bring values of require_conce within contact_form.php.

what's interesting if change require_once include works.

any ideas?

thanks

assuming filepath relative theme directory, should best way include it...

function subscribe_custom_php_shortcode($atts, $content = null) {     $atts = shortcode_atts(         array(             'filepath' => '/index.php'         ), $atts );      ob_start();     include get_stylesheet_directory() . $atts['filepath'];     $content = ob_get_clean();      return $content; } add_shortcode('custom_php', 'subscribe_custom_php_shortcode'); 

as can see... use get_stylesheet_directory function current theme directory.

hope helps 😉


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -