How to call Wordpress snippets in external/independent PHP file (outside of Wordpress)

StackOverflow https://stackoverflow.com/questions/14316019

  •  15-01-2022
  •  | 
  •  

Вопрос

I'm trying to set up an independent PHP file which has the ability to use a shortcode of a plugin currently installed. This file is to be completely external from my current Wordpress installation.

I'm doing this as the php file (and ultimately plugin shortcode functionality) needs to be called by ESI as my website is being handled by Varnish.

The plugin I'm using: http://wordpress.org/extend/plugins/custom-content-by-country/

My current and unsuccessful attempt at making this work:

<?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

?>

Any help would be greatly appreciated. Thanks

Это было полезно?

Решение

Did you try to include wp-blog-header.php instead?

Your external file:

define( 'WP_USE_THEMES', false );        
require('wp-blog-header.php');       

do_shortcode('[CBC country="us" show="n"]');

I just tried the following and it worked:

External file:

define( 'WP_USE_THEMES', false );        
require('wp-blog-header.php');       

do_shortcode('[displayshortcode]');

functions.php file:

function shortcode_function() {
     echo 'shortcode';
}
add_shortcode('displayshortcode', 'shortcode_function');
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top