How to use wordpress shortcodes in custom php file folder? - php

I want to display wp formcraft (plugin) in my custom PHP file so I wanted to use "do_shortcode()" function but nothing works it just shows white screen.
my php file code:
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
include_once $path . '/wp-config.php';
include_once $path . '/wp-load.php';
include_once $path . '/wp-includes/wp-db.php';
include_once $path . '/wp-includes/pluggable.php';
define('WP_USE_THEMES', false);
echo apply_filters( "the_content","[fc id='1'][/fc]");
?>

I don't really get what your doing, but i guess you want to do something like this:
function xyz() {
do_shortcode("[fc id='1'][/fc]");
}
echo apply_filters("the_content", "xyz");

Related

problem with using dirname(__FILE__ ,2) in php

I try to use dirname(FILE ,2)
I have index.php file in "public" directory
Project
|--app
--config
|--init.php
--public
|--css
--js
--index.php
--resources
|--inc
--langs
--favicon.ico
in index.php
<?php
$pageTitle = "Home";
define('__BASEDIR__', dirname(__FILE__ ,2) );
echo __BASEDIR__ . '<br>';
require_once (__BASEDIR__ . '/config/init.php');
?>
this print: ...\xampp\htdocs\project
in init.php:
<?php
$templ = __BASEDIR__ . '/resources/inc/';
$lang = __BASEDIR__ . '/resources/langs/';
$css = __BASEDIR__ . '/public/css/';
$js = __BASEDIR__ . '/public/js/';
echo $templ .'<br>';
echo $lang .'<br>';
echo $css .'<br>';
echo $js .'<br>';
require_once $templ . 'header.php';
require_once $templ . 'navbar.php';
it shows in the browser
...\xampp\htdocs\project
...\xampp\htdocs\project/resources/inc/
...\xampp\htdocs\project/resources/langs/
...\xampp\htdocs\project/public/css/
...\xampp\htdocs\project/public/js/
Note this ...\project/.... /* backslash */
The browser cannot find files (style.css, script.js....)
Firstly
if the difference in slashes bothers you, you can set your path's using DIRECTORY_SEPARATOR like so
From
$templ = __BASEDIR__ . '/resources/inc/';
to
define('DS', DIRECTORY_SEPARATOR);
$templ = __BASEDIR__.DS.'resources'.DS.'inc'.DS;
Secondly You will not be able to use that relative pathing to load browser resources like js and css files. You need to look into absolute path.
Example: Say your on windows, the path $templ will be something along those lines C:\xampp\htdocs\project\public\css\file.css which will not work for loading your browser resources. If your document root is C:\xampp\htdocs\project\ you will need to use /public/css/file.css to load your css or js files.

After wordpress upgrade I'm getting wp_user not found in pluggable.php

I am calling a php script from an app that checks the credentials of a wp user. This worked fine until I upgraded wordpress from 4.3 to 4.4 (and 4.5). It's really time I got this sorted but I can't think why wp-user is no longer available as it is in the include list.
error: wp_user not found in pluggable.php
Please see the code below..
define( 'SHORTINIT', TRUE );
require_once $abspath . '/wp-load.php';
require_once $abspath . '/wp-includes/user.php';
require_once $abspath . '/wp-includes/pluggable.php';
require_once $abspath . '/wp-includes/formatting.php';
require_once $abspath . '/wp-includes/capabilities.php';
require_once $abspath . '/wp-includes/kses.php';
require_once $abspath . '/wp-includes/meta.php';
require_once $abspath . '/wp-includes/l10n.php';
require_once $abspath . '/wp-includes/class-wp-error.php';
require_once $abspath . '/wp-includes/general-template.php';
require_once $abspath . '/wp-includes/link-template.php';
$the_authenticate = wp_authenticate_username_password('null',$user_name,$user_password);
if( is_wp_error( $the_authenticate ) ) {
echo '{"error":"The username was not recognised"}';
}
else
{
$the_user_authenticate_id = $the_authenticate->ID;
$the_user = get_user_by('login', $user_name);
$the_user_id = $the_user->ID;
if ( !$the_user )
{
//echo "{'error':'The username was not recognised'}";
}
I have found the solution is that I now need to include the following..
require_once $abspath . '/wp-includes/class-wp-roles.php';
require_once $abspath . '/wp-includes/class-wp-user.php';
require_once $abspath . '/wp-includes/class-wp-role.php';
Please can anyone explain why these extra includes are suddenly necessary in wordpress 4.4 and whether my solution makes sense?
Download the updated wordpress version from wordpress.org and replace your current wp-admin and wp-includes folders with updated wordpress folders.
Hope it will work.

How do I include a path of google client library in php

$r = set_include_path(get_include_path() . '\google-api-php-client-master\src');
echo $r;
require_once 'Google/Client.php';
require_once 'Google/Service/AdExchangeSeller.php';
function __autoload($class_name) {
include 'examples/' . $class_name . '.php';
}
when I am running it on browser its showing following error..
You are not using the path seperator when calling set_include_path, try this:
$r = set_include_path(get_include_path() . PATH_SEPARATOR . '\google-api-php-client-master\src');
Here is the documentation on set_include_path
Edit
Did you restart Apache after making the change to your php.ini?

PHP Load every class in a directory

I have coded a PHP script that includes every file in a directory. But im wondering if there is a way to load the classes in the files im including like a autoloader or something?
<?php
define("include_dir", dirname(__FILE__) . '/includes/');
foreach (scandir(include_dir) as $filename)
{
if (is_file(include_dir . '/' . $filename))
{
//its a php file, lets do this!
if (substr($filename, -4) == '.php')
{
include include_dir . $filename;
}
}
}
?>
try this-
function __autoload($class_name) {
include $class_name . '.php';
}
$obj = new MyClass1();
$obj2 = new MyClass2();
the manual -
http://php.net/manual/en/language.oop5.autoload.php

Why isn't wordnik's php api loading the required classes?

I'm getting the following error with wordnik's php api
Fatal error: Class 'Example' not found in Swagger.php on line 212
I've edited Swagger.php to get it to work, below is the original function
function swagger_autoloader($className) {
$currentDir = substr(__FILE__, 0, strrpos(__FILE__, '/'));
if (file_exists($currentDir . '/' . $className . '.php')) {
include $currentDir . '/' . $className . '.php';
} elseif (file_exists($currentDir . '/models/' . $className . '.php')) {
include $currentDir . '/models/' . $className . '.php';
}
}
The working code I changed to is
function swagger_autoloader($className)
{
include $currentDir . '/models/' . $className . '.php';
}
MORE INFO
My file structure is as follows.
WORDNIK (contains start.php)>>>WORDNIK (contains Swagger.php)>>MODELS (contains Example.php)
I'm using wampserver 2.2 with php 5.4.3
UPDATE/EDIT
Using the following code
function swagger_autoloader($className) {
echo "dirname(__FILE__)= ",dirname(__FILE__);
$currentDir = substr(__FILE__, 0, strrpos(__FILE__, '/'));
echo "currentDir=".$currentDir."</br>";
echo "className=".$className."</br>";
echo "__FILE__=",__FILE__."<br/>";
die();
I get the results
dirname(__FILE__)=D:\wamp\www\wordnik\wordnik
currentDir=
className=Example
__FILE__=D:\wamp\www\wordnik\wordnik\Swagger.php
As suggested by vcampitelli, using either __DIR__ or dirname(__FILE__) works.
MY QUESTION
Why doesn't the original function work?
UNIMPORTANT INFO
For people struggling through the examples, here is my start.php file
<?php
require('./wordnik/Swagger.php');
require('./wordnik/WordApi.php');
require('./wordnik/AccountApi.php');
require('./wordnik/WordsApi.php');
require('./wordnik/WordListApi.php');
require('./wordnik/WordListsApi.php');
$myAPIKey = 'replace_this_with_your_real_api';
$client = new APIClient($myAPIKey, 'http://api.wordnik.com/v4');
$wordApi = new WordApi($client);
$example = $wordApi->getTopExample('irony');
print $example->text;
?>
What does $currentDir return? Have you tried using __DIR__ or dirname(__FILE__) (they are the same) instead of that substr?
If you are using the second example just as you posted (without declaring $currentDir), so that is the problem at your original code: $currentDir is not returning the right folder!
With the original code, which file is being included? Because, actually, I think no one is! Use echo inside those if statements to check that!
The original function doesn't work because it is looking for the position of a UNIX-style forward slash '/', whereas you are on Windows and have backslashes '\'. That's a bug! I'll fix the lib to use dirname(FILE) as you do. Thanks for pointing out the error.

Categories