import a class from theme directory outside of wordpress.
The CRON
I am cronning a daily job via Cpanel for my site that will feed a stats table. I created a file in the root Home/myDirectory (same level as public_html ) beloo is the code
<?php
include dirname(__FILE__) . '/public_html/wp-load.php'; // Load WP Functions
include dirname(__FILE__) . '/public_html/wp-content/themes/cooking/config/Data/ViewsData.php'; // require a class
function test(){
if(class_exists('ViewsData')){
$viewsData = new ViewsData;
$views= $viewsData::getViews(394);
update_post_meta(394 , 'views_test', $views);
}
die();
}
test();
THE CLASS
as shown in the code I am trying to include a class from the theme folder. this class include different functionaries to set , get and update the views data> bellow is an idea of how the class is structured ;
namespace GS\Data;
use GS\DisplayFunc;
class ViewsData {
static function getViews(){}
}
however class_exists('ViewsData') always return false.
any suggestions on what is wrong. or even on how to reorganize the whole cron solution. the most important is that I need to use many classes from my theme folder.
I was able to find the problem. it has to do with namespaces the code that works bellow :
<?php
include dirname(__FILE__) . '/public_html/wp-load.php'; // Load WP Functions
include dirname(__FILE__) . '/public_html/wp-content/themes/cooking/config/Data/ViewsData.php'; // require a class
function test(){
if(class_exists('GS\Data\ViewsData')){
$viewsData = new ViewsData;
$views= $viewsData::getViews(394);
update_post_meta(394 , 'views_test', $views);
}
die();
}
test();
Related
I am building a custom Gutenberg block an register the editor script as recommended in the block.json file.
"editorScript": "file:./index.js",
Now I would like to pass data from php to Javascript using the wp_add_inline_script() function. However this requires the handle of the script. In the block.json I don't name a handle and there doesn't seem to be a possibility to do it.
Is there something like a default handle? Or is there another way to pass data from my php plugin file to JavaScript (I want to pass the plugin directory path).
Any help is highly appreciated!
Yes, wp_add_inline_script() is used for passing data from PHP to JavaScript, including Gutenberg blocks.
The script handle you are looking for is the name of your block with the / replaced with a -, eg:
block.json
{
"name": "my/blockname",
"editorScript": "file:./index.js",
...
}
my-block.php
function my_blockname_add_inline_script()
{
// Plugin path for my block
$path = plugin_dir_url(__FILE__);
$handle = 'my-blockname'; // name from block.json with a '-' instead of '/'
$data = 'const myBlockPath ="' . $path . '";';
$position = 'before';
wp_add_inline_script($handle, $data, $position);
}
/**
* Registers the block using the metadata loaded from the `block.json` file.
*/
function my_blockname_block_init() {
register_block_type( __DIR__ . '/build' );
// Enqueue script after register_block_type() so script handle is valid
add_action('admin_enqueue_scripts', 'my_blockname_add_inline_script');
add_action('wp_enqueue_scripts', 'my_blockname_add_inline_script');
}
add_action( 'init', 'my_blockname_block_init' );
For some reason, I can't get working the WordPress function get_editable_roles(). Is there a bug with this? Doesn't mind where I add, in the functions.php, in the index.php, in the top, in the bottom. It always gives the undefined function error.
I added this line of code in Twenty Fifteen theme, in a fresh WordPress install and without any plugin:
$role = get_editable_roles();
It give this error:
Error: Call to undefined function get_editable_roles()...
If I load the user.php file before, then it works:
if (!function_exists('get_editable_roles')) {
require_once(ABSPATH . '/wp-admin/includes/user.php');
}
You should consider to load user.php even you are in admin side, especially you have created a custom admin page like options.
if ( ! function_exists( 'get_editable_roles' ) ) {
require_once ABSPATH . 'wp-admin/includes/user.php';
}
$roles = get_editable_roles();
It is also discussed here.
I think the problem is that you're trying to use this function outside the admin section where it isn't loaded.
From the Wordpress documentation:
Notes
The file that defines this function (wp-admin/includes/user.php) is only loaded in the admin sections.
See: https://codex.wordpress.org/Function_Reference/get_editable_roles
I try to call from my Codeigniter controller as outside of the box my Wordpress blog menu, but I get the following error
Fatal error: Cannot redeclare site_url() (previously declared in /../system/helpers/url_helper.php:53)
public function getWordpressMenu() {
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
// no theme output
define('WP_USE_THEMES', false);
// initializes the entire Wordpress
require $_SERVER['DOCUMENT_ROOT'] . 'blog/wp-blog-header.php';
wp_nav_menu('your_theme_menu_location');
}
I know both of frameworks have the same function but is there a way to disable on of inside of controller?
In Codeigniter you can simply replace your helpers by creating a new file at application/helpers
In your case, you can simply create a new file at application/helpers with name url_helper.php
Just copy the url_helper.php from the folder system/helpers and copy it to application/helpers/ . Now Open the file url_helper.php and rename the function url_helper to something else, for example: ci_site_url
I have a function that's doing something awesome, and when this finishes the work it should delete a folder and it's not. Here's what and how:
This is where I hook my function and it works
add_action('option_after_ajax_save', 'run_custom_function');
This is where I have my function
function run_custom_function() {
global $wp_rewrite, $wp_filesystem;
$my_folder = ABSPATH . 'wp-content/themes/' . $themename . '/inc/my-folder/';
//other code
//now once everything else is done and everything works to this point, delete the folder
unlink($my_folder); // I also tried rmdir($my_folder); and it's not working
}
Do you have any idea?
at the moment I am working on a Wordpress theme with a functions.php that will include multiple helper functions files. I want this theme to be very flexible and customizable if a child theme is made based on the theme.
I have this piece of code currently in my functions.php:
add_action( 'template_redirect', 'wpf_contact' );
/**
* Include the logic/settings for tpl-contact.php.
*
* Includes the logic/settings for tpl-contact.php. It will load the child's
* mail.inc.php first if that is available.
*/
if ( ! function_exists( 'wpf_contact' ) ) {
function wpf_contact() {
if ( is_page_template( 'tpl-contact.php' ) && is_child_theme() && file_exists( get_stylesheet_directory() . '/inc/wpf/mail.inc.php' ) ) {
include( get_stylesheet_directory() . '/inc/wpf/mail.inc.php' );
} else {
include( get_template_directory() . '/inc/wpf/mail.inc.php');
}
} // end wpf_contact()
}
What the code above does is load mail.inc.php from child if the files exist. If it doesn't load it from the parent theme.
Cause I am having multiple files I want to load like that. I was thinking it there might not be a easier way of doing this? Is there a built in function that does this in WordPress?
Or should I just adjust the above function and add parameter's so it can be used for other files as well? Not sure if this is efficient.
To answer your question directly based on your sample, i would not check to see if the file has been placed in the child theme. If a child-theme developer has customized the file, they can simply declare the function and include their own file (wherever they place it). There is no risk of code duplication because your sample function is simple enough.
Even if you simplified your code for the purposes of placing it here....you can always keep includes wrapped in a very simple function that can be declared by a child theme. This will keep PHP running efficient and not having to check for files all the time.