Just working on a custom wordpress plugin. It will have an export and import function that need to be run at very exact times. So for those 2 functions I am ditching the wp-cron and set up cron jobs via crontab.
But how do I run only those 2 functions in this plugin but nothing else?
Thanks for your help.
I am not sure but this ideas might work.
You can create custom php file at root of wordpress installation and add
<?php
define('WP_USE_THEMES', false);
require("wp-load.php");
Now you can use all wordpress and plugin functions. Then you can set this file to execute by your webhost cron.
Other than that in functions.php of your theme whatever is outside function will be always executed. so you can set some GET parameter and check in functions.php for that parameter in url and execute a function. You need to set that parameter in cron job url at your webhost cpanle.
Related
Im using the WordPress Multisite MU-Plugins directory to run some custom functions which i need to be
available to ALL subsites in my network. When i run the function in the primary sites' functions.php
file it runs as expected but it obviously cannot access any subsites.
Is the problem with the loading order that WP utilises - i read here the order is:
the WordPress core code
mu-plugins
plugins
functions.php
the theme code for the specific template being displayed
I believe the issue may be that im trying to call wp funcs that are not yet loaded yet. Is there a way to defer the loading of 'Must-Use' -plugins until, say when functions.php runs??
You can't defer the load, no, but you can move the code in the mu-plugin that requires on other things to be loaded into action hooks for actions that are triggered later in the load order:
hook plugins_loaded to run code after all plugins have loaded
hook after_setup_theme to run code after the theme has loaded
(I guess that's what you mean by 'functions.php' ?)
However I think all of WordPress's own functions are loaded before mu-plugins are loaded, so this may not be your problem.
I have a custom PHP file on which I want to use some WordPress functions such as wp_get_current_user(). I've tried requiring wp-load.php, but that increases the load time significantly as it loads all WP functions.
Is there anyway I can use only the functions I need from WP?
Thanks, Emir
You can create page template or plugin then you able to access wp_get_currunt_user() function in you template file or plugin file
I want to write a plugin so that I can connect to it with an app bot, and store information inside the database.
Note that because I directly connect to php file inside this plugin, the public WordPress values like $wpdb are not known.
And how can i access to wp functions? such as get_option,...
thanks for your help.
Maybe this helps you
require_once( 'path to wordpress project root'. '/wp-load.php' );
// Set up the WordPress query.
wp();
For example if you want to create plugin with name - test
You need to create a directory with name 'test' inside plugin directory wp-content/plugins/test
Then you need to create file test.php - it is the main file of your plugin
Then you need to add at beginning of your test.php file after comments require wp-load if WordPress environment is not included (it is your case when you call your file directly)
For example, your plugin main file is wp-content/plugins/test/test.php starts with this snippet and before this snippet, you need to place your plugin metadata like name - description and so on. I recommend to download WordPress and see default plugins meta in wp-content/plugins/askimet/askimet.php and wp-content/plugins/hello.php.
if ( !defined('ABSPATH') ) {
/** Set up WordPress environment */
require_once( '../../wp-load.php' );
}
then you can use get_option() $wpdb and other wp functions.
wp() function in my previous answer need for initializing global wp query. It is not necessary. You can create your own wp query by using WP_Query class.
https://codex.wordpress.org/Class_Reference/WP_Query - there is description with examples how to use WP_Query.
Then You can write your plugin in OOP or functional style. You can write your plugin analogically to Askimet for example or with boilerplates. This is the question about plugin development. Hope this question helps you achieve your purpose.
https://wordpress.stackexchange.com/questions/85486/is-there-any-plugin-development-framework
Hope this help you
I am trying to access a plugin file directly in the browser, so i can run a cron.
When i go to the correct url, i am getting a page not found error.
Does wordpress by default prevent you from accessing this directly? Is it maybe something to do with the .htaccess, or should i be able to view this directly?
I am trying to access the file located like this (just an example):
http://mywebsite.com/wp-content/plugins/askimet/askimet.php
Any help would be greatly appreciated!
WordPress does not prevent you from accessing PHP files directly. However, the PHP files themselves usually do. This basically makes sure WordPress is loaded.
In your example, akismet.php has the following
// Make sure we don't expose any info if called directly
if ( !function_exists( 'add_action' ) ) {
echo 'Hi there! I\'m just a plugin, not much I can do when called directly.';
exit;
}
You probably do want WordPress to be loaded for your plugin, though.
Instead, you need to make your plugin know of a URL construct which you can detect and intercept. For example, say you visit the page example.org/?my-plugin-action. Your plugin should check for this on normal page requests (such as init or template_redirect) and if it is found, call your PHP script, then call exit; so WordPress does not try to load a page.
function my_plugin_action() {
if ( !isset($_GET['my-plugin-action']) ) return;
echo 'Run cron task here.';
exit;
}
add_action( 'init', 'my_plugin_action' );
To recap: Don't call your plugin directly. Make a URL that displays your content, then exit the script before WordPress tries to display the default page.
There is no such restriction by default in WordPress. You can access a file directly in WordPress if you want to.
See if the file code contains something like below -
if ( ! defined( 'ABSPATH' ) )
//some action if accessed directly
Otherwise, check back the URL and make sure your path is correct.
One more thing, if you will access the plugin file directly and it contains any WordPress core function, it will give error since in that case, WordPress core doesn't get loaded.
However, there are some non recommended ways to load WordPress core.
I am new with drupal. I have installed ultimate cron in Drupal 7. now I have created a php script to be a mycron.php, my problem is that I don't know where can I insert my cron job in my Drupal's ultimate cron.
I am looking where can I insert mycron.php to ultimate cron module page but I failed.
I also look to the site where i downloaded it but it is not there. http://drupal.org/project/ultimate_cron
Does anyone have an idea how to solve my problem?
Usually you don't add custom cron scripts (full .php files) but defining the job that needs to be done using hook_cron()
If you create a module with "myname" as the name and you can now define a function like
function myname_cron(){
//do stuff here
}
You can use full Drupal functions here as a fully boostrapped Drupal instance available for you. and Ultimate Cron (or other cron-related modules) can take care of load balancing now.