我想创建一个简码来呈现自定义的PHP页面。如果我使用wordpress模板,我会得到非常奇怪的行为。使用WordPress的简码来渲染PHP
例如。如果我使用代码创建模板:
<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-## -->
然后在wordpress页面中,我只需输入文件的位置,例如, /contact/contact_form.php。 contact_form.php页面在wordpress主题中的屏幕上呈现。
现在,我最近发现了短码,并认为这些将是在页面上呈现php的更好方式。而不是有页面的内容为/contact/contact_form.php我可以有[custom_php filepath =“/ contact/contact_form.php”]
这就是我遇到的问题。 contact_form.php好像在整个地方都有许多require_once()函数,尽管第一个方法(上面的方法)工作正常,但它们似乎并不存在。
对于我的简码的代码,我有:
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');
正如你所看到的,代码非常相似,除了我加入ob_start()和ob_get_clean(),以获得基于某人的意见PHP的结果。删除这些仍然不会带回contact_form.php中的require_conce的值。
有趣的是,如果我改变require_once以包含它的作品。
任何想法?
感谢
您是使用您购买的模板还是默认的wp主题? – Eduardo
我自己写的一个模板如上 – Ant