WordPress execute specific cronjob on specific Page - php

I am having a small tour page that uses built-in cron jobs (wp-cron) to sync tours and sessions via an API every 4 hours and until here everything is perfect.
Now, what I am trying to achieve now is to also and "forcefully" execute this specific cronjob (regardless of it's schedule) when being on a specific page, e.g. Thank you page (not Woocommerce).
What's the best way to have this achieved? Some expert help would be greatly appreciated, thank you.

I think you can try by using admin_init hooks and check the page id or url then run the execute function.
add_action( 'travel_booking_do_tour_search_action', 'my_custom_cron_function');
function my_custom_cron_function(){
// your execute action
}
add_action( 'admin_init', 'execute_cron_on_specific_page' );
function execute_cron_on_specific_page{
if( is_page( 2094 ) ) // page_id = 2094 (eg:thank you page)
{
my_custom_cron_function();
}
}

I have been left with the doubt, are you using the native Cron job of your server or the wp-cron of WordPress?

Related

How to automatically: WordPress permalink settings set via a plug-in, after the "plugins_loaded" action

By selecting the desired persistent connection structure:
Which is like this: http://localhost/%postname%/
Thanks to the following code of WordPress,
<? php add_action ('plugins_loaded', 'function_name'); ?>
I expect the above persistent connection settings to update itself automatically every time.
For this, we need to login to the admin panel and save the settings each time. But isn't there a way to automate it?
After a long period of research and testing, the results are very satisfactory. It successfully adjusts as you wish and works properly. Excellent.
function reset_permalinks()
{
global $wp_rewrite;
$wp_rewrite->set_permalink_structure( '/%postname%/' );
}
add_action( 'plugins_loaded', 'reset_permalinks' );

Executing custom php file from Cron, implementing Wordpress mail send

I'm building a sort of selfservice portal, and need to implement it on a wordpress site. The portal itself, is build in pure php, jquery, sql etc. Without the use of the Wordpress libraries, which the rest of the main site rests on.
I've searched the web, trying to find what i need, but i couldn't find a match.
So.. What am i trying to do..
I need to run a Cron job every X day at X clock, triggering a custom PHP file in the root on the server (lets call it portal_reminder.php), which then uses the built in (or a plugin?) to send e-mails to the target specified in the custom PHP file.
Oh, as the server is hosted in a serverpark using multi-hosts, i'm not allowed to install any "external" programs (sendmail), nor can i create custom cronjobs in the terminal (cron -e).
So, i need a wordpress cron plugin to do the cron handling, aswell as wordpress/other to handle the e-mails.
For clarification, my idea was as follows:
Cronjob triggers "portal_reminder.php"
Portal reminder triggers "mailsender", including $to, $from, $content (html content)
"mailsender" sends the mail :)
Is this even possible?
Cron jobs works well with WordPress, even if a server cron is better. WordPress cron will trigger when someone visits your WordPress site.
Here is an example with a new schedule (WordPress native schedules are only daily, twicedaily, hourly):
add_filter( 'cron_schedules', 'wc_dsr_add_custom_cron_schedule' );
function wc_dsr_add_custom_cron_schedule( $schedules ) {
$schedules['fourdaily'] = array(
'interval' => 21600, // 86400s/4
'display' => __( 'Four time daily' ),
);
return $schedules;
}
function wc_dsr_create_daily_backup_schedule(){
//Use wp_next_scheduled to check if the event is already scheduled
$timestamp = wp_next_scheduled( 'wc_dsr_cron_send_action' );
//If $timestamp == false schedule daily backups since it hasn't been done previously
if( $timestamp == false ){
//Schedule the event for right now, then to repeat daily using the hook 'wc_create_daily_backup'
wp_schedule_event( time(), 'fourdaily', 'wc_dsr_cron_send_action' );
}
}
//Hook our function , wc_dsr_cron_send_action(), into the action wc_dsr_cron_send_report
add_action( 'wc_dsr_cron_send_action', 'wc_dsr_cron_send_report' );
function wc_dsr_cron_send_report(){
// do your job
wp_mail('test#test.com', 'Subject', 'Message');
}
In your case, wc_dsr_cron_send_report() might include and work with portal_reminder.php. To work well with WordPress function, you must add this to portal_reminder.php
define( 'WP_USE_THEMES', false );
require_once('pathto/wp-load.php);
You'll be able to find more example on the web.

Multiple pages with same URL in WordPress

I have a WordPress site that has a standard Page called Places with URL
example.com/places/
And I have several child Pages called by cities
example.com/places/city-1
example.com/places/city-2
Now, I have a custom post type Place that should indicate a single place and should have permalink like
example.com/places/place-1
But then if I go to one of the previous links with city-1, city-2 I get 404 obviously because there is no place with that permalink.
Is there a way for WordPress to drop to previous permalink. So if there is no place with that name, look for a page with it.
You could probably use the the REFERER-value from the PHP server variable $_SERVER, but it´s not very reliable and can be altered.
I am using the plugin "Permalink Finder" in one of the pages I am maintaining and that works quite well for finding changed URL. You could give it a try and see if it works for you, too.
In case somebody ever having a similar problem, it can be done by using verbose page rules. This is an example I found at WordPress Stack Exchange
https://wordpress.stackexchange.com/questions/22438/how-to-make-pages-slug-have-priority-over-any-other-taxonomies-like-custom-post
add_action( 'init', 'wpse16902_init' );
function wpse16902_init() {
$GLOBALS['wp_rewrite']->use_verbose_page_rules = true;
}
add_filter( 'page_rewrite_rules', 'wpse16902_collect_page_rewrite_rules' );
function wpse16902_collect_page_rewrite_rules( $page_rewrite_rules )
{
$GLOBALS['wpse16902_page_rewrite_rules'] = $page_rewrite_rules;
return array();
}
add_filter( 'rewrite_rules_array', 'wspe16902_prepend_page_rewrite_rules' );
function wspe16902_prepend_page_rewrite_rules( $rewrite_rules )
{
return $GLOBALS['wpse16902_page_rewrite_rules'] + $rewrite_rules;
}

How to use a custom post type archive as front page in WordPress?

I'd like to use a custom post type archive as a site's front page, so that
http://the_site.com/
is a custom post type archive displayed according to my archive-{post-type}.php file.
Ideally I would like to alter the query using is_front_page() in my functions.php file. I tried the following, with a page called "Home" as my front page:
add_filter('pre_get_posts', 'my_get_posts');
function my_get_posts($query){
global $wp_the_query;
if(is_front_page()&&$wp_the_query===$query){
$query->set('post_type','album');
$query->set('posts_per_page',-1);
}
return $query;
}
but the front page is returning the content of "Home" and seems to be ignoring the custom query.
What am I doing wrong? Is there a better way, in general, of going about this?
Note: I did post this in WordPress Answers but that community is comparatively tiny.
Posting the final solution as an answer (Isaac placed it as a comment) just for anyone still looking.
Isaac was on the right track with adding a filter via functions.php. What he did wrong was call is_front_page(), which doesn't work yet because we're in pre_get_posts and the query hasn't been executed yet.
We do have the current page ID however. So we can still solve this, by looking into the WordPress option register for an option called page_on_front, which returns the ID of the page the user set as frontpage.
(For an overview of all WordPress options, just visit <yourwordpressinstallation>/wp-admin/options.php in your browser.)
Which makes for the following solution as suggested by Ijaas:
add_action("pre_get_posts", "custom_front_page");
function custom_front_page($wp_query) {
// Compare queried page ID to front page ID.
if(!is_admin() && $wp_query->get("page_id") == get_option("page_on_front")) {
// Set custom parameters (values based on Isaacs question).
$wp_query->set("post_type", "album");
$wp_query->set("posts_per_page", -1);
// WP_Query shouldn't actually fetch the page in our case.
$wp_query->set("page_id", "");
}
}
You might have to alter a few conditional tags to make sure all plugins still work, depending on how heavily the query gets altered.
Hope this helps someone.
Update: as noted below, add !is_admin() to the if-statement to make sure the function only runs on the frontend. If you only want this action to run for the initial query, you could also add the main query check $wp_query->is_main_query().
In response to Robberts solution:
answered May 6 '13 at 12:04
adding && !isAdmin() to the if function other wise it replaces the post type query for all admin pages as well.
Just in case anyone has issues with this.
edit:
also adding && $wp_query->is_main_query() to the if statement stops it affecting widgets and the menu
so total code I have
if($wp_query->get("page_id") == get_option("page_on_front") && !isAdmin() && $wp_query->is_main_query()) {}
In order to get that query to work, you're going to have to add that code to a page template, create a page, set your template as the template for the page you just created, and then set that page as the home page in Settings => Reading in the admin area.
By the way, the is_front_page() function only returns true if you're on a page that's been set as the home page from the admin menu.
The other option would be to modify index.php, but if you did that, is_front_page() would always return false. In that case, you'd want to use is_home() instead.
I hope that helps.
Isaac, you are correct, I didn't thoroughly read your question and I made the assumption that you were looking to do this the "easy" way.
Anyway, I put your code on my test site and, indeed, it didn't work. I looked at my SQL log and it turns out that your code produces this in the query wp_posts.post_status = 'private'.
So, I tried adding the line $query->set('post_status', 'public'); to your function, and it worked just fine.
In summary, try this:
add_filter('pre_get_posts', 'my_get_posts');
function my_get_posts($query){
global $wp_the_query;
if(is_front_page()&&$wp_the_query===$query){
$query->set('post_type','album');
$query->set('posts_per_page',-1);
$query->set('post_status', 'public');
}
return $query;
}

Wordpress plugin: Hook on custom url

I want to make a plugin, that I will use for some jQuery AJAX loading of table data.
I have a function that prints the data correctly, but how do I "hook" into a specific url?
Like say, I want the function to be run, and the data to be printed whenever a request to /mycustomplugin/myurl.php is run? (Please note that the url/file should not exist)
I have no experience with WP plugins.
To filter your custom URL before Wordpress starts executing queries for other things use something like this:
add_action('parse_request', 'my_custom_url_handler');
function my_custom_url_handler() {
if($_SERVER["REQUEST_URI"] == '/custom_url') {
echo "<h1>TEST</h1>";
exit();
}
}
A simple
if ($_SERVER["REQUEST_URI"] == '/mycustomplugin/myurl.php') {
echo "<my ajax code>";
}
Should work wonders.
If you wanted to return regular wordpress data you could just include wp-blogheader.php into your custom php file like so
//Include Wordpress
define('WP_USE_THEMES', false);
require('Your_Word_Press_Directory/wp-blog-header.php');
query_posts('showposts=10&cat=2');
Just use regular theming tags to return the content you desire. This
Where is your table data coming from though? Are you trying to show this information on the admin side or the viewer side?
Also see for a full breakdown of calling hooked functions with wp_ajax http://codex.wordpress.org/AJAX_in_Plugins
add_action( 'init', 'my_url_handler' );
function my_url_handler() {
if( isset( $_GET['unique_hidden_field'] ) ) {
// process data here
}
}
using add_action( 'init', 'your_handler') is the most common way in plugins since this action is fired after WordPress has finished loading, but before any headers are sent. Most of WP is loaded at this stage, and the user is authenticated.

Categories