I have several WordPress sites hosted in my server, and some of them have the W3 Total Cache plugin installed. Right now I flush the cache for each site manually, but I want to do it programatically and all together.
I'm trying to write a PHP script that will do this. I was looking at the code used by the "Clean Cache All" option for this plugin in the dashboard, and is this piece here:
if ( $modules->plugin_is_enabled() ) {
$menu_items['10010.generic'] = array(
'id' => 'w3tc_flush_all',
'parent' => 'w3tc',
'title' => __( 'Purge All Caches', 'w3-total-cache' ),
'href' => wp_nonce_url( network_admin_url(
'admin.php?page=w3tc_dashboard&w3tc_flush_all' ),
'w3tc' ));
So I'm guessing there are 2 ways to do what I need:
1- Make a web request, but that would probably require authentication.
2- Instance the class and use the method w3tc_flush_all();
This is what I have thus far:
function clean_cache($web)
{
global $path;
$path2 = $path.$web["directory"]."/httpdocs/wp-content/plugins/w3-total-cache/";
if(file_exists($path2)) //Tienen TotalCache instalado
{
require_once $path.'w3-total-cache-api.php';
//if(class_exists("w3-total-cache-api.php"))
//{
$plugin_totalcache = & w3_instance("w3-total-cache-api.php");
//if(function_exists('w3tc_dbcache_flush'))
$plugin_totalcache->w3tc_fush_all();
//}
}
}
Any help will be much appreciated.
Related
I am new to PHP programming and I am trying to teach myself WordPress theme development for fun and I am using PhpStorm as my IDE.
I am trying to better understand the inner-workings of WordPress and I am running into a roadblock on something.
I have a sandbox plugin that I created to use to play around with WordPress.
In my “wp-content/plugins/sandbox/sandbox.php” file, I am just running basic PHP code to help me get used to the language as it relates to WordPress.
Also, I installed both Kint and Whoops using Composer to help with debugging.
Now that I got that out of the way, here is what I am doing:
Code #1
namespace MyDevPlayground\Sandbox;
add_action( 'loop_start', __NAMESPACE__ . '\process_the_string' );
function process_the_string() {
$current_user = wp_get_current_user();
$data_packet = array(
'id' => $current_user->ID,
'email' => $current_user->user_email,
'name' => array(
'first_name' => $current_user->user_firstname,
'last_name' => $current_user->user_lastname,
),
);
render_user_message( $data_packet );
}
function render_user_message( array $current_user ) {
$user_id = $current_user['id'];
d( "Welcome {$current_user['name']['first_name']}, your user id is { {$user_id} }." );
ddd( "Welcome {$current_user['name']['first_name']}, your user id is {$user_id}." );
}
When I run Code #1 above everything is fine and Kint displays the values just fine.
Now for the problem I am having that I don’t understand about WordPress:
Code #2
namespace MyDevPlayground\Sandbox;
add_action( 'loop_start', __NAMESPACE__ . '\check_logged_in_user' );
function check_logged_in_user(){
$current_user = wp_get_current_user();
if ( 0 == $current_user->ID ) {
d('Not logged in');
} else {
ddd('Logged in');
}
}
check_logged_in_user();
When I run Code #2 above, Whoops reports the following error:
Call to undefined function MyDevPlaygroundSandbox\wp_get_current_user
For some reason when I run Code #1, the wp_get_current_user() function loads just fine, but not with Code #2.
Can someone help me understand why this is in laymen’s terms if possible?
What is the difference between Code #1 and Code #2?
How come the wp_get_current_user() function is not loading in Code #2, but it is in Code #1?
Thank you for your help.
when you use "add_action" command you can not use function name for calling that action,
you need to use the call command like this :
do_action("check_logged_in_user");
more information : https://developer.wordpress.org/reference/functions/add_action/
tl;dr; Trying to link the 'save and return' button when editing/deleting a course to my local plugins index.php instead of moodles default redirect for these features, moodle allready has a returnTo query parameter so i was thinking if that could be used somehow.
Hey
I am creating a local plugin that has a administration panel, where you can access CRUD on all courses in the system as seen in the picture below:
The problem now is that whenever I click edit, I get into the course edit page of course, but when I return from there I click "save and return" I would like to get back to my own admin page instead of the course page or category manage page.
The code I have right now looks like this:
//edit
$edit_course_moodle_url = new moodle_url('/course/edit.php', array('id' => $course->id, 'returnto' => 'local/adminpanel/index.php'));
$edit_course_url = $edit_course_moodle_url->get_path() . '?id=' . $edit_course_moodle_url->get_param('id') . '&returnto=' . $edit_course_moodle_url->get_param('returnto');
//delete
$delete_course_moodle_url = new moodle_url('/course/delete.php', array('id' => $course->id, 'returnto' => 'local/adminpanel/index.php'));
$delete_course_url = $delete_course_moodle_url->get_path() . '?id=' . $delete_course_moodle_url->get_param('id') . '&returnto=' . $delete_course_moodle_url->get_param('returnto');
As you can see I use the "returnto" query parameter, normally moodle has a "catmanage" as "returnto" that returns you to the category management page, where moodle has its own CRUD for categories and courses. So my question is, can I create my own alias for a link and use it like moodle uses the catmanage link, but for my admin page instead.
Thanks a lot ! :)
EDIT:
Change code to the following:
if (empty($CFG->loginhttps)) {
$securewwwroot = $CFG->wwwroot;
} else {
$securewwwroot = str_replace('http:','https:',$CFG->wwwroot);
}
$returnurl = new moodle_url($securewwwroot . '/local/adminpanel/index.php');
$edit_course_moodle_url = new moodle_url($securewwwroot . '/course/edit.php', array(
'id' => $course->id,
'sesskey' => sesskey(),
'returnto' => 'url',
'returnurl' => $returnurl->out(false))
);
$edit_course_url = $edit_course_moodle_url->out();
But it looks like moodle took away the button from edit course called "save and return" now it only has "save and display" or "Cancel" , both of which brings me back to the course, sad times :(
According to the code I can see in course/edit.php, you should use the following URL arguments:
returnto: 'url'
returnurl: The url
sesskey: sesskey()
In code that gives us:
$returnurl = new moodle_url('/local/plugin/page.php');
$editurl = new moodle_url('/course/edit.php', array(
'id' => 2,
'sesskey' => sesskey(),
'returnto' => 'url',
'returnurl' => $url->out(false)
));
echo $editurl->out();
The page course/delete.php does not seem to support those arguments. But it's probably easier for your plugin to delete the course by itself, it's as simple as calling delete_course($courseid);.
Hey can anyone explain to me why I get this error when I try to save the registration form.
Fatal error: Cannot unset string offsets in /home/wolf/public_html/wp-content/plugins/userpro/admin/admin-functions.php on line 252
Here is the line in question for the php.
/** List all groups **/
function userpro_admin_list_groups(){
global $userpro;
$output = null;
$groups = $userpro->groups;
unset($groups['register']);
unset($groups['edit']);
unset($groups['view']);
unset($groups['login']);
unset($groups['social']);
$array = array(
'register' => __('Registration Fields','userpro'),
'edit' => __('Edit Profile Fields','userpro'),
'login' => __('Login Fields','userpro'),
'social' => __('Social Fields','userpro')
);
IF anyone can shed some liht on this that would be great as far as i see theres nothing written wrong, and even the plugin maker is scrating his head on this....
the config for the php is 5.4
And the Apache is 2.4
Mysql is running 5.6
$actions = array(
'EDIT' => sprintf('Edit',
'abf_cm',
'edit_course',
$item['course_id'],
'thickbox edit-box',
'edit_'.$item['course_id']
),
'DELETE' => sprintf('Delete','course_management','do_process','delete',$item['course_id']),
);
In doing so, the edit part is not being displayed.Am i doing anything wrong.
I also tried using the placeholders
'EDIT' => sprintf('Edit',
'abf_cm',
'edit_course',
$item['course_id'],
'thickbox edit-box',
'edit_'.$item['course_id']
),
but still no results. I also noticed that when i remove the class and id attributes in the earlier version, then it works fine.
Can you please give me a satisfactory explanation of this and tell me where am i doing wrong.
EDIT:
Im using this inside Wordpress for creating custom table using WP_List_Table class
function column_course_name($item ) {
//Build row actions
$actions = array(
'EDIT' => sprintf('Edit',
'abf_cm',
'edit_course',
$item['course_id'],
'thickbox edit-box',
'edit_'.$item['course_id']
),
'DELETE' => sprintf('Delete','book_management','do_process','delete',$item['course_id']),
);
//Return the title contents
return sprintf('%1$s%3$s',
/*$1%s*/ strlen($item['course_name'])>0?$item['course_name']:'<span style="color:silver">(No Name)</span>',
/*$2%s*/ $item['course_id'],
/*$3%s*/ $this->row_actions($actions) //row_actions is a method in this class
);
}
update:
Well, its strange to mention but the code works when i use a single class( ie when i delete the space between the two classes for the tag) .
Any thoughts?
Dipesh, maybe you have errors in the code around this snippet.
Try to check your code in isolation. I copied your code to the separate .php script with little set-up and checked $actions array with print_r, like this:
edit_array.php
<?php
$item = array();
$item['course_id'] = 1;
$actions = array(
'EDIT' => sprintf('Edit',
'abf_cm',
'edit_course',
$item['course_id'],
'thickbox edit-box',
'edit_'.$item['course_id']
),
'DELETE' => sprintf('Delete','course_management','do_process','delete',$item['course_id']),
);
print_r($actions);
I ran this script from console and got the following results:
$ php edit_array.php
Array
(
[EDIT] => Edit
[DELETE] => Delete
)
Generated link for $actions['EDIT'] is HTML valid, so one can safely conclude that your code itself is working fine, and error lies somewhere else.
A node loads a profile of a user (external database + views). All of this works when I visit: node/123/profile/id/3. Now I have implemented hook_menu() in order to load any profile page and have nicer URLs.
When I load it myself for some reason $left in page.tpl.php is suddenly empty and many more variables seem not to be loading. I have tried many different functions to render and create the correct $output but realized that node_show() seems to be the function of choice.
Testing has shown now that for some reason hook_nodeapi() calls are ignored.
My code:
/**
* Implementation of hook_menu
*/
function modulename_menu() {
$items = array();
$items['my/nice/url/profile'] = array(
'description' => 'This page holds a view that shows profiles based on the %',
'page callback' => 'website_profile_load',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Menu path wildcard callback
*/
function website_profile_load() {
$output = node_show(node_load(1221), false, true);
return $output;
}
So what is the correct way to do this and get Panels (see comment below) to load correctly?
UPDATE:
I am using table wizard and Views 2 to connect to another database with information about people that aren't users of the system. This is an alumni page, the page is administered externally and showed internally (nothing I can do about, have to make this work :)
Just discovered that Panels aren't loaded at all. So even if the node I am trying to load is uses panels for some reason none of that is loaded.
/**
* Menu path wildcard callback
*/
function website_profile_load($uid = null) {
if (!$uid) {
global $user; // if no user passed in argument, show current user profile
$uid = $user->uid;
}
$output = drupal_render(content_profile_show_profiles($uid));
}
There are many reasons why rendering the result of a node_load is different from going to the stock Drupal path /node. It is way too much to go over here honestly but the short answer is that you have to define a template/theme and blocks etc for each page you create. Just because you make a new path and do a node_load in the callback for that path doesn't mean Drupal can automagically know how you want to display that content. It simply loads data from the node and it is available to do whatever you please with it after that. This is why you get a blankish looking page instead of what you'd expect from going through /node.
However I will offer this simple solution since it sounds like you want the exact same page you'd get when you go to 'node/123/profile/id/3' but accessible through a link you define yourself. You just need to setup a redirect in your hook_menu like so:
$items['my/nice/url/profile'] = array(
'description' => 'This page holds a view that shows profiles based on the %',
'page callback' => 'drupal_goto',
'page arguments' => 'node/123/profile/id/3',
'access callback' => TRUE,
'type' => MENU_CALLBACK);
This is essentially saying that when you navigate to 'my/nicer/url/profile' it runs: drupal_goto('node/123/profile/id/3');
I found the answer to be that apparently somewhere in the pipeline of creating a node Drupal uses $path (that is originally set by $_GET['q']) and sometimes also $_GET['q'] to determine how to render the page. NOTE that I am using Panels and Ctools Page Manager modules in order to get my things working correctly.
It turns out Panels, if you search the code, looks at $_GET['q'] for quite an amount of things.
Here is what I ended up with:
/**
* Implementation of hook_menu
*/
function modulename_menu() {
$items = array();
// For department and having nice URL's for their profile pages.
$items['my/nice/url/profile/%'] = array(
'description' => 'This page holds a view that shows profiles based on the %',
'page callback' => 'website_profile_load',
'page arguments' => arg(4),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Menu path callback
*/
function website_profile_load($id = NULL) {
// Rename the query internally since other functions base the design
// on the way the query is structured and not simply by the node which
// is currently loading.
if(!empty($id)) {
$path = $_GET['q'] = 'node/1221/profile/id/' . $id;
}
// Use ctools function to correctly display node view since
// this site heavily uses ctools rendering for panels and
// web parts / web pages.
drupal_load('module', 'page_manager');
ctools_include('node_view', 'page_manager', 'plugins/tasks');
if(function_exists('page_manager_node_view')) {
$output = page_manager_node_view(node_load(1221));
} else {
// Will display incorrectly but still load the UI
$output = node_page_view(node_load(1221));
}
return $output;
}
And it works :)