This is from inside my Wordpress plugin, inside the main file:
function my_plugin_install() {
$my_site_url = get_site_url();
$my_options['my_site_url'] = $my_site_url;
// Save
}
register_activation_hook(__FILE__, 'my_plugin_install');
Currently, the install is successful but the 'my_site_url' option is not saved. I'm assuming because the way I'm using the $my_options array at this point doesn't mean anything. It should save this data to the wp_options table.
I can't seem to get this to save, or even find a way to test this as using "echo" gives Wordpress an error during install. Is there a best method for running a script and updating the database during install?
Thanks in advance.
You need to use the WordPress function update_option to save your option value:
function my_plugin_install() {
$my_site_url = get_site_url();
update_option('my_site_url', $my_site_url);
}
register_activation_hook(__FILE__, 'my_plugin_install');
And then later, when you need that value, you can use get_option:
$my_site_url = get_option('my_site_url');
*UPDATE
Since it appears you want to manage multiple of your own options, then I suggest using a simple "utility" function, like so:
function update_my_option($key, $value) {
// Load all of the option values from wp_options
$all_options = get_option('my_options');
// Update just the one option you passed in
$all_options[$key] = $value;
// Save to wp_options
update_option('my_options');
}
And, an appropriate getter function:
function get_my_option($key, $default = NULL) {
// Load all of your options from wp_options
$all_options = get_option('my_options');
// Return just the one option you are asking for
return (isset($all_options[$key])) ? $all_options[$key] : $default;
}
Then, rather than calling update_option directly, you'll call this function, as illustrated below:
function my_plugin_install() {
$my_site_url = get_site_url();
update_my_option('my_site_url', $my_site_url);
}
And, to get one of your options:
$my_site_url = get_my_option('my_site_url');
Related
Is there method to use wp all export php functions to replace data or maybe any other statement we can use directly inside custom XML export.
What i need is to achieve this logic
I have custom taxonomy in XML called {Conditions} which return value of each post as "Used" "New"
Values appear correctly, but where i need to export XML required different values then those above, which i need to replace during export.
Logic:
If {Conditions} = "Used"
Set Used = "20";
Elseif{Conditions} = "New"
Set New = "10";
WP ALL EXPORT PLUGIN
Please take a look at "Pass Data Through PHP Functions" doc:
"...you can click on the export field and then use the "Export the value returned by a PHP function" option."
https://www.wpallimport.com/documentation/export/pass-data-through-php-functions/
You would specify the function name into the text field as in the screenshot on the documentation page (convert_conditions) or, if running a custom XML export, call the function from the field with something like this:
[convert_conditions({Conditions})]
...and provide the function in the function editor for the field:
function convert_conditions($cond_from_xml = null) {
if($cond_from_xml === 'Used') {
return '20';
} elseif($cond_from_xml === 'New') {
return '10';
} else {
return '0';
}
}
Can anyone advice how best to set a temporary variable with scope to be used between hooks?
I have a custom module with the purpose of checking and warning if an attempted file upload matches an existing file on the system.
I am using hook_form_alter to identify the specific upload form and hook_file_validate to check if the file was previously uploaded.
My problem is those two hooks don't share a common parameter - I need to pass information from hook_form_alter back to hook_validate.
The information I want the functions to share is a simple boolean which should be destroyed immediately the file upload is done/dismissed, so using variable_set to persist it to the database is overkill. I don't believe a session or cookie approach is best either.
UPDATES:
Globals approach:
function duplicate_uploads_warning_init(){
$GLOBALS['processed'] = 'testing';
}
function duplicate_uploads_warning_file_validate($file){
drupal_set_message("PROCESSED[file-validate]: {$GLOBALS['processed']}", 'warning');
}
function duplicate_uploads_warning_form_alter(&$form, &$form_state, $form_id){
if( $form_id == 'file_entity_add_upload' ){
drupal_set_message("PROCESSED[form-alter]: {$GLOBALS['processed']}", 'error');
$GLOBALS['processed'] = 'tested';
$form['#validate'][] = 'duplication_validate';
}
}
The code above sets GLOBALS[processed] in the init hook and that value in immediately confirmed in the hook_form_alter.
However the attempt to reassign the value to tested fails. The reassigned value is what I hoped to see in hook_file_validate but I still get the initial value of testing.
Hook_form_alter validation approach:
I tried adding a custom validation function but the upload of the image still took place where I intend to stop it. My code is as follows:
function duplication_validate($form, &$form_state) {
$data = duplicates($form_state['complete form']['upload']['#file']->filename);
if( sizeof($data) > 0 ){
form_set_error('test', 'testing validation');
return false;
}
}
I can confirm my $data variable has content and the sizeof test returns greater than 0.
using variable_set to persist it to the database is overkill
I don't agree they are overkill (you can easily delete variables), and as #2pha have mentioned globals are not recommended. I think you could use variable_set($name, $value), variable_get($name), and variable_del($name) without worrying unless you need to super-optimise your sites database queries. If you really don't want to use the variable_* functions, then maybe cache_set() and cache_get() might work because you can give it a temporary status.
Edit, variables approach:
function duplicate_uploads_warning_init(){
$account = \Drupal::currentUser();
variable_set($account->id() . '_processed', 'testing');
}
function duplicate_uploads_warning_file_validate($file){
$account = \Drupal::currentUser();
$processed_var = variable_get($account->id() . '_processed');
drupal_set_message("PROCESSED[file-validate]: {$processed_var}", 'warning');
}
function duplicate_uploads_warning_form_alter(&$form, &$form_state, $form_id){
if( $form_id == 'file_entity_add_upload' ){
$account = \Drupal::currentUser();
$processed_var = variable_get($account->id() . '_processed');
drupal_set_message("PROCESSED[form-alter]: {$processed_var}", 'error');
variable_set($account->id() . '_processed', 'tested');
$form['#validate'][] = 'duplication_validate';
}
}
… and in a success or #submit callback function run variable_del($account->id() . '_processed') to delete the variable.
maybe $GLOBALS['processed'] was initialized with the static keyword elsewhere....?
So i created a plugin that is basically a gallery that you choose options as you go.
First choose Brand
Then Color
Then Style
At each step i am passing the variables via $_GET
So once you have chosen your brand and continue the next page URL is cabinets/?brand=1
Then after you choose your color it is cabinets/?brand=1&color=2
i have written a rewrite for this that is supposed to make pretty urls but all it is doing is showing the home page.
add_filter('rewrite_rules_array','cabinets_rewrite_rules_array');
function cabinets_rewrite_rules_array($rules){
$cabinets_slug = 'cabinets';
$my_cab_rules[$cabinets_slug.'/?$'] = $cabinets_slug."/?brand=$matches[1]";
$my_cab_rules[$cabinets_slug.'/(.+?)/?$'] = $cabinets_slug."/?brand=$matches[1]&color=$matches[2]";
$my_cab_rules[$cabinets_slug.'/(.+?)/(.+?)/?$'] = $cabinets_slug."/?brand=$matches[1]&color=$matches[2]&style=$matches[3]";
return $my_cab_rules + $rules;
}
i have tried many things even as much as updating the htaccess file but i dont want to have to do that since this is a plugin.
Any Idea?
You could try adding your new variables to the list of query vars.
function prfx_add_query_vars($aVars) {
global $wp_query;
$aVars[] = "brand";
$aVars[] = "color";
$aVars[] = "style";
return $aVars;
}
add_filter('query_vars', 'prfx_add_query_vars');
So this is the problem I am running into. If I have a comment object, I want to create a renderable array that is using the display settings of that comment. As of now this is what I have:
$commentNew = comment_load($var);
$reply[] = field_view_value('comment', $commentNew, 'comment_body', $commentNew->comment_body['und'][0]);
Which works fine because I dont have any specific settings setup for the body. But I also have image fields and video embed fields that I need to have rendered the way they are setup in the system. How would I go about doing that?
Drupal core does it with the comment_view() function:
$comment = comment_load($var);
$node = node_load($comment->nid);
$view_mode = 'full'; // Or whatever view mode is appropriate
$build = comment_view($comment, $node, $view_mode);
If you need to change a particular field from the default, use hook_comment_view():
function MYMODULE_comment_view($comment, $view_mode, $langcode) {
$comment->content['body'] = array('#markup' => 'something');
}
or just edit the $build array received from comment_view() as you need to if implementing the hook won't work for your use case.
How would one go about deleting all option names in a WordPress database beginning with a specific prefix?
I would assume we need to specify a prefix, get all options that begin with that prefix, and then delete each option found.
Here is a sample of the prefix and WP functions for getting and deleting options in the database.
<?php
$prefix = 'cpt_';
$getOpt = get_option($prefix);
foreach($getOpt as $toDelete){
$deleteOpt = delete_option($prefix);
if(!$deleteOpt){
echo 'Failure.';
}
if($deleteOpt){
echo 'Success.';
}
}
?>
Resources:
http://codex.wordpress.org/Function_Reference/delete_option
http://codex.wordpress.org/Function_Reference/get_option
You could run this query:
global $wpdb;
$wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE 'cpt_%'" );
or put it in a function like so:
function delete_options_prefixed( $prefix ) {
global $wpdb;
$wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '{$prefix}%'" );
}
delete_options_prefixed( 'cpt_' );
You need to make a "whitelist" of all the variables your plugin sets (I assume you are looking at a plugin uninstall script), then just loop through it at the other end so you can delete them all.
Something as simple as:
// Somewhere in your plugin, maybe as a class property
$pluginDefinedOptions = array('my_name', 'my_created', 'my_modified'); // etc
// Clear up our settings
foreach($pluginDefinedOptions as $optionName) {
delete_option($optionName);
}
This is the only way to keep your plugin code tidy.