【问题标题】:How to call Wordpress snippets in external/independent PHP file (outside of Wordpress)如何在外部/独立 PHP 文件(Wordpress 之外)中调用 Wordpress 片段
【发布时间】:2013-01-14 17:50:06
【问题描述】:

我正在尝试设置一个独立的 PHP 文件,该文件能够使用当前安装的插件的简码。此文件将完全位于我当前安装的 Wordpress 之外。

我这样做是因为 ESI 需要调用 php 文件(以及最终的插件简码功能),因为我的网站正在由 Varnish 处理。

我正在使用的插件:http://wordpress.org/extend/plugins/custom-content-by-country/

我目前和不成功的尝试使这项工作:

<?php

// This section is experimental and not working (also the 'do_shortcode...' sections below)

include ("../../../wp-load.php");
include ("../../../wp-includes/shortcodes.php");

// end of section

$cwd = getcwd();
$path = substr($cwd, 0, strpos($cwd, 'wp-content/'));
require $path . 'wp-blog-header.php';
global $adrotate_config;

do_shortcode('[CBC country="us" show="y"]'); // not working correctly
echo adrotate_group('1'); // Working Correctly
do_shortcode('[/CBC]'); // not working correctly

do_shortcode('[CBC country="us" show="n"]'); // not working correctly
echo "Hello World!"; // Working Correctly
do_shortcode('[/CBC]'); // not working correctly

?>

任何帮助将不胜感激。谢谢

【问题讨论】:

    标签: php wordpress varnish esi


    【解决方案1】:

    您是否尝试改为包含wp-blog-header.php

    您的外部文件:

    define( 'WP_USE_THEMES', false );        
    require('wp-blog-header.php');       
    
    do_shortcode('[CBC country="us" show="n"]');
    

    我刚刚尝试了以下方法并且成功了:

    外部文件:

    define( 'WP_USE_THEMES', false );        
    require('wp-blog-header.php');       
    
    do_shortcode('[displayshortcode]');
    

    functions.php 文件:

    function shortcode_function() {
         echo 'shortcode';
    }
    add_shortcode('displayshortcode', 'shortcode_function');
    

    【讨论】:

    • 感谢您提供解决方案。我在尝试让您的代码与我的简码代码一起正常工作时遇到了一些麻烦:$sShortcodeText = '[CBC country="us" show="y"]' . adrotate_group('1') . '[/CBC]'; do_shortcode( $sShortcodeText ); - 我不知道该放在哪里? - 你必须原谅我有限的 PHP 知识,对不起。如果可能的话,你能提供一个可行的例子吗?保罗
    • 你试过echo do_shortcode('[CBC country="us" show="y"]' . adrotate_group('1') . '[/CBC]' ); 吗?
    • 完美!非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多