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?
Related
I am trying to run a simple cron job using a custom hook but it does not run. It get's scheduled (viewing via wp cron plugin) but doesn't run. If I change the hook out to wp_loaded for example it runs fine. This is in a themes functions.php file:
// Schedule event.
if (!wp_next_scheduled ('my_hourly_event')) {
wp_schedule_event(time(), 'hourly', 'my_hourly_event', array(), true);
}
add_action('my_hourly_event', 'do_this_hourly', 10); // Works with action being 'wp_loaded'
function do_this_hourly() {
error_log("test"); // Not working.
$log = __DIR__ . '/error_log.txt';
file_put_contents($log, "Response: \n", FILE_APPEND); // Works with making action 'wp_loaded'
}
I have a few other websites using similiar code with custom hooks and they work no problem. What am I doing wrong here? Did a wordpress update break cron jobs with custom hooks? I am on wordpress version 5.7.1
Try with init hook.
function schedule_my_cron(){
wp_schedule_event(time(), 'hourly', 'my_hourly_event', array(), true);
}
if(!wp_next_scheduled('my_hourly_event',$args)){
add_action('init', 'schedule_my_cron');
}
add_action('my_hourly_event', 'do_this_hourly', 10); // Works with action being 'wp_loaded'
function do_this_hourly() {
error_log("test"); // Not working.
$log = __DIR__ . '/error_log.txt';
file_put_contents($log, "Response: \n", FILE_APPEND); // Works with making action 'wp_loaded'
}
I working on my plugin and tried to override some templates.
If I visit the page portfolio my screen gives a whitepage.
This is my code
define("PLUGIN_DIR_PATH", plugin_dir_path(__FILE__));
add_filter( 'template_include', 'plugin_tweak_template', 99);
function plugin_tweak_template( $template ) {
if ( is_page('portfolio')) {
$template = PLUGIN_DIR_PATH . 'required/templates/portfolio.php';
}
return $template;
}
I use this code in my plugin root file.
I think the define path has a conflict.
define("PLUGIN_DIR_PATH", plugin_dir_path(__FILE__));
PLUGIN_DIR_PATH is a example in many tutorials but you can define this one time.
Is you have another plugin check the define name from this plugins if this is the same name you have a conflict.
Remember always: use variable names etc. by your own and prevent issues.
I must to add new options and functions in post pages in admin panel. I call a new function in edit-form-advanced.php and edded this function in template.php file. The question is this wrong? Becouse my function is in one file with functions on wordpress. Or maybe must be in other file? but where i must call it?
For wp-content part i know and i make a child theme of parent theme, but i do not know what to do when i must add code in wp-admin part.
example:
edit-form-advanced.php
do_custom_boxes( null, $post );
and in template.php
function do_custom_boxes( $screen, $object ) {
global $wpdb;
$appTable = $wpdb->prefix . "post_panel";
$query = $wpdb->prepare("SELECT * FROM $appTable WHERE post_id = ".$_GET['post']." ", $screen);
$applications = $wpdb->get_results($query);
......
}
Short answer: Yes, it's wrong to do so. Whenever you update your WordPress you'll loose all your changes.
WordPress allows you to hook into its code, modify its behavior and many things.
Please read about actions and filters.
Basically, Actions allow you to fire a function when something happens in WordPress.
For example:
<?php
function do_something_when_admin_pages_init() {
// Do something here
}
add_action('admin_init', 'do_something_when_admin_pages_init')
Filters allow you to modify data/output of another function. It's like it let you step in the middle, do something with the data and then continue.
Example from the WordPress page:
<?php
function wporg_filter_title($title) {
return 'The ' . $title . ' was filtered';
}
add_filter('the_title', 'wporg_filter_title');
This modifies the title before it's printed.
So with those two ways of 'hooking' into the WordPress code, you can write your code in your theme's functions.php file, or write a Plugin (it's up to you).
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();
I am writing a plugin that will take advantage of other plugin's features (think about a plugin for a plugin).
My file lies in /plugins/new-plugin/new-plugin.php
and I need to make a
include(/plugins/OLD_plugin/old-plugin.php)
so I can use a couple of functions from the old-plugin.php file.
What is the correct way to do this? I could maybe make the functions in old-plugin.php available globally, but I don't want to change the old-plugin.php file.
I've already tried several ways to do this, but none worked. The new-plugin will only show some info in an options page, not viewable for the general public and does not interact with any public page or post in my site.
I've already tried $_SERVER, WP_PLUGIN_DIR, WP_CONTENT_DIR, the absolute server path, relative paths and even some black magic, but nothing seems to work good.
With some of this solutions the plugin's options page shows good but the blog's pages do not render. With other solutions the inverse happens, and with some other solutions nothing even render, be it admin pages or blog's pages, all with errors regarding to file not found.
The new-plugin.php is as simple as
<?php
/*
WP Common Headers
*/
global $wpdb;
if ( ! defined( 'WP_CONTENT_DIR' ) )
define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );
if ( ! defined( 'WP_PLUGIN_DIR' ) )
define( 'WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins' );
include '/server-absolute-path/public_html/gameblogs/wp-content/plugins/old-plugin/old-plugin.php';
add_action('admin_menu', 'new_plugin_menu');
function new_plugin_menu() {
$page_title = 'New Plugin';
$menu_title = 'New Plugin';
$function = 'new_plugin_admin_page';
$menu_slug = 'new_plugin';
add_menu_page($page_title, $menu_title, 0, __FILE__, $function);
}
function new_plugin_admin_page() {
$result = old_plugin_link_data(" WHERE link_destination NOT LIKE '/%' AND link_destination NOT LIKE '%gameblogs%'");
$total = count($result);
old_plugin_list_links($result, $total, FALSE, FALSE);
*/
}
?>
thanks for any ideas!
check the old plugin files and see if there are any do_actions or apply_filters in it. If there are then you can hook into the old plugin script with your new plugin using add_action and apply_filters and execute other things you want to do.
see http://codex.wordpress.org/Function_Reference/do_action
and http://codex.wordpress.org/Function_Reference/apply_filters
For example (very basic example):
If in old plugin you find a:
do_action('some_type_of_reference);`
In your new plugin you can hook into it by doing:
`add_action('some_type_of_reference', 'name_of_my_function');
function name_of_my_function() {
//executed code here
}`
If in old plugin you find a:
apply_filters('some_type_of_reference', $variable);
Then in your new plugin you can hook into the filter by doing:
apply_filter('some_type_of_reference', 'my_function');
function my_function( $variable ) {
//act on the variable from the filter.
return $variable;
}
Have you looked at the plugins_url function? I haven't had an in-depth read through your code, but it might help.
The plugins_url template tag retrieves the url to the plugins directory or to a specific file within that directory. You can hardcode the plugin slug in $path or pass FILE as a second argument to get the correct folder name.
Hope this helps!