In php, I must check if the first attribute of a PHP object is null. the problem is I do not know the key because the object is set dynamicaly and can be an Object A or Object B etc...
if( empty( $this->editable_item->item ) || $this->editable_item->item->? === null ){ <-- ? the key can be various things
//Do something like log an error
}
How can check the first attribute without knowing its key ?
To do this, you need to use the function get_object_vars() and use reset like this :
$a_vars = get_object_vars( $this->editable_item->item );
reset( $a_vars );
$first_key = key( $a_vars );
if( empty( $this->editable_item->item ) || empty( $a_vars[$first_key] ) ){
//Do something like log an error
}
Related
I’m integrating a form of $_POST method with ajax filtering, to filter through repeater filed in all posts. so i’m not filtering posts (always all posts are displayed), just filtering inside each post.
the repeater field contain two sub fields – multiselect and Wysiwyg Editor.
Now, according to what the user selected in the front-end form, if it’s equal to a post multiselect values, it should display the Wysiwyg Editor field of the matching multiselect.
I have this working without multiselect. but with the multiselect, i can’t get a conditional to be ALL selected filters values, to be the exact match of ALL multiselect values.
So the result i get, as it is in a foreach loop, is multiple Wysiwyg Editor fields.
I tried a lot of things, this is an example code of one of them:
(‘cond_options’ – multiselect
‘description’ – Wysiwyg Editor’
‘ weather/sky/night’ – filters values)
if ( have_rows('cond-repeater') ):
while (have_rows('cond-repeater') ) : the_row();
$select_options = get_sub_field('cond_options');
$selectdesc = get_sub_field('description');
if( $select_options ):
foreach( $select_options as $select ):
if( isset( $_POST['weather'] ) && $_POST['weather'] && isset( $_POST['sky'] ) && $_POST['sky'] && isset( $_POST['night'] ) && $_POST['night'] == $select ){
echo $selectdesc;
}
echo $select; //just to see the output of selected options
endforeach;
endif;
endwhile;
endif;
Without knowing much about your data and the actual end result you are trying to achieve. I have put together something that might help steer you in the right direction.
The main thing to point out is the use of in_array, with this we can check if a value exists in the multiple select without having to loop through it with a foreach.
Let me know if this helps.
$_weather = !empty( $_POST[ 'weather' ] ) ? $_POST[ 'weather' ] : null;
$_sky = !empty( $_POST[ 'sky' ] ) ? $_POST[ 'sky' ] : null;
$_night = !empty( $_POST[ 'night' ] ) ? $_POST[ 'night' ] : null;
if ( have_rows('cond-repeater') ) {
while ( have_rows('cond-repeater') ) {
the_row();
if ( $options = get_sub_field( 'cond_options' ) ) {
// Check payload includes all required parameters
if ( $_weather && $_sky && $_night ) {
// Check if all parameters exist in the multiselect value
if ( in_array( $_weather, $options ) && in_array( $_sky, $options ) && in_array( $_night, $options ) ) {
echo get_sub_field( 'description' );
} else {
echo $select; // Warning: Undefined variable!
}
}
}
}
}
I'm working on my first WordPress theme option from scratch. I have some code like below that fills the empty options if they don't set 'in current send form process'...
(Without this code, if a user sends the form then the other options that aren't in the current view will be clear and their value will be deleted! This code keeps their values and is working correctly.)
I just need to simplify this code:
if ( empty( $options['index1_status'] ) ) {$options['index1_status'] = get_status('index1_status');}
if ( empty( $options['index2_status'] ) ) {$options['index2_status'] = get_status('index2_status');}
if ( empty( $options['index3_status'] ) ) {$options['index3_status'] = get_status('index3_status');}
if ( empty( $options['index4_status'] ) ) {$options['index4_status'] = get_status('index4_status');}
if ( empty( $options['index5_status'] ) ) {$options['index5_status'] = get_status('index5_status');}
How can I simplify this code with a loop or function?
And just add more to the $optionsArray
$optionsArray = [
'index1_status',
'index2_status',
'index3_status',
'index4_status',
'index5_status',
];
foreach ($optionsArray as $v)
{
// if $options[$v] is empty, then set it to get_status($v), else keep its original value
$options[$v] = (empty($options[$v])) ? get_status($v) : $options[$v];
}
I am working on a some code that will post information from custom field to the database.
The following code checks to see if the $meta_key is present:
// Apply filters to keys
$this->keys = apply_filters('sc_meta_keys', $this->keys);
// See if the current meta_key is in the array keys
if($this->in_array_recursive($meta_key, $this->keys)) {
if($action == "add") {
//information from array gets added to DB
}
}else{
//Do something else
}
I am however unsure how to check if there is a value in the meta_key, as I don't want the if-statement to run if there is no value present.
The array will always have keys however not every post will necessarily have the custom fields filled.
Probably you should do this. This would check if $metakey exists .
// See if the current meta_key is in the array keys
if (isset( $meta_key )) && (is_array($meta_key) && ($this->in_array_recursive($meta_key, $this->keys))) {
You should always check if a variable is set and utlize short circuit operators to reduce the processing time of the conditionals. As You haven't included the logic for where you are setting $meta_key I'll check if it's set or not to be sure.
$this->keys = apply_filters('sc_meta_keys', $this->keys);
// See if the current meta_key is in the array keys
if( isset( $meta_key ) && ! empty( $this->keys ) && $this->in_array_recursive( $meta_key, $this->keys ) ) {
if($action == "add") {
//information from array gets added to DB
}
}
else {
//Do something else
}
I need to run an encryption function on each member of an associative array prior to it being saved to wp_postmeta table. My form allows dynamic add/delete of rows. I've been trying to get this to work using array_walk().
This is how the form is configured:
<input type="text" id="z_my_data[][username]" name="z_my_data[0][username]">
<input type="text" id="z_my_data[][password]" name="z_my_data[0][password]">
This is ran on the 'save_post` action:
// Save encrypted data to post meta
if (isset($_POST['z_my_data'])) {
// Get posted form variables
$my_data = $_POST['z_my_data'];
// Encrypt each member of each row
for ($i = 0; $i < count($my_data); $i++) {
$cryptKey = $this->cryptKey;
array_walk($my_data[strval($i)], create_function('&$val', 'global $cryptKey; $val = Crypto::encrypt($val, $cryptKey);'));
}
if ( ! add_post_meta( $post_id, '_my_data', $my_data, true ) ) {
update_post_meta( $post_id, '_my_data', $my_data);
}
} else {
delete_post_meta( $post_id, '_my_data' );
}
I can see that the data is being encrypted. Here is a print_r($my_data) after the for loop:
Array
(
[0] => Array
(
[username] => ®ØåÛâÏ0…"ë°?mˤÙ
[password] => xSFç„L¶·3z˜'J0ÖRÅÎj
)
)
But the post meta key is not created and no error is generated. The meta key doesn't exist in the postmeta table, yet add_post_meta() returns false and the key/value is never added.
Does anyone see what I'm doing wrong?
I'm not sure how to tell, but I think the [0] is a named key and not an index key. I say that because I can create more than one and delete the [0] element and the single element remaining still shows [1] using print_r.
In your if ( ! add_post_meta( $post_id, '_my_data', $my_data, true ) ) { you are using add_post_meta() function and last argument is set to true.
It should be false instead, because you are inserting an array and NOT a string.
For this reason your if statement is not working properly.
Instead, your code should be (as add_post_meta last argument default value is false):
if ( ! add_post_meta( $post_id, '_my_data', $my_data ) ) {
update_post_meta( $post_id, '_my_data', $my_data );
}
Alternatively, you could also use this:
if ( ( !empty( get_post_meta( $post_id, '_my_data' ) ) ) {
update_post_meta( $post_id, '_my_data', $my_data );
}
References:
WordPress Code Reference - add_post_meta
WordPress Code Reference - update_post_meta
WordPress Code Reference - get_post_meta
what i'm currently trying to achieve is some simple theme options. What i want to b able todo is;
Under the Wordpress GUI, Theme options section which i've currently got a few setup (with no actions behind them), is to simply "Click a check box which will disable front page widgets, if it's on then show them if it's off disable them"
I understand the logic behind, but i have no idea on how to create something will will return a simple true of false, from the theme-options.php, link it into the front-page.php and use the result from theme-options to be the condition to show said area.
Any help with this would be amazing.
<?php $options = get_option( 'SV_theme_options' ); ?>
<input id="SV_theme_options[option1]" name="SV_theme_options[option1]" type="checkbox" value="1" <?php checked( '1', $options['option1'] ); ?> />
/**
* Sanitize and validate input. Accepts an array, return a sanitized array.
*/
function theme_options_validate( $input ) {
global $select_options, $radio_options;
// Our checkbox value is either 0 or 1
if ( ! isset( $input['option1'] ) )
$input['option1'] = null;
$input['option1'] = ( $input['option1'] == 1 ? 1 : 0 );
// Say our text option must be safe text with no HTML tags
$input['sometext'] = wp_filter_nohtml_kses( $input['sometext'] );
// Our select option must actually be in our array of select options
if ( ! array_key_exists( $input['selectinput'], $select_options ) )
$input['selectinput'] = null;
// Our radio option must actually be in our array of radio options
if ( ! isset( $input['radioinput'] ) )
$input['radioinput'] = null;
if ( ! array_key_exists( $input['radioinput'], $radio_options ) )
$input['radioinput'] = null;
// Say our textarea option must be safe text with the allowed tags for posts
$input['sometextarea'] = wp_filter_post_kses( $input['sometextarea'] );
return $input;
}
Also in the front-page.php I've required_once for this file, tried calling by doing:
echo theme_options_validate( $input['option1' );
however all i get is the word Array returned.
Regards,
Ben
Maybe that's because you made a typo.
You didn't close the bracket
echo theme_options_validate( $input['option1' );
Should be:
echo theme_options_validate( $input['option1'] );