How can I simplify this code with a loop or function? - php

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];
}

Related

Wordpress+ACF - Filtering through multiselect options in a repeater field

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!
}
}
}
}
}

How to check a dynamic attribute of one object in PHP ?

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
}

Wordpress Theme Development

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'] );

Not managing to find a way to use is_page() with a variable inside storing an array

Building a Wordpress plugin here. I have a text form in which user can insert pages IDs - separated by commas - to display a BAR in it.
Then I set a variable to get those Ids and I am trying to use this variable with the conditional tag "is_page( array() )".
Problem is, the BAR is showing only on the page related to the first ID given by the user. It is strange, cause I echoed the variable and it is showing all Id's given, separated by commas. Hardcoding the IDs, like this "is_page( array(207,306) )", I get the result I want and the BAR is displaying on the 2 pages related to the IDs. But when I use "is_page( array($setting_value) )", only page related to the first ID is showing the BAR, even when "echo $setting_value" results in 207,306.
Can anybody show me what is wrong?
Here's the parts of the code related to it:
//getting the IDs
$setting_value = esc_attr( (get_option( 'ma-singular' ) ) );
if ( $home != 'onlyHome' ) {
echo "<input class='text' type='text' name='ma-singular' value='" . $setting_value . "' />";
}
//displaying the BAR on the PAGES related to the IDs typed by users
$singular = esc_attr (get_option ( 'ma-singular' ));
if ( $home != 'onlyHome' && is_page( array($singular) ) ) {
if ( is_user_logged_in() && !empty($alttext) ) {
echo '<div class="ma-' . $cor = get_option( 'ma-cor') .
'" style="background-color:' . $cordefundo = get_option( 'ma-fundo' ) .
'">' . $alttext . '</div>';
}
You need to explode $singular before using it. It's going wrong because what you think is an array of values is a single value inside the array.
Let's say for example 'ma-singular' = 127,135,189.
array( $singular ) would be equal to array( '127,135,189' ) when what you need is array( 127, 135, 189 ).
So to fix this you would do:
$singular = esc_attr (get_option ( 'ma-singular' ));
if ( ! empty( $singular ) ) {
$singular = explode( ',', $singular );
}
if ( $home != 'onlyHome' && is_page( $singular ) ) {

Using PHP to reorder includes based on priority / order number

I am using wordpress, and I am creating a "widget chooser" for every page of the site. On the backend of each page, I have 15 checkboxes - if the user chooses a checkbox, it shows that particular widget in the sidebar of that page.
I have a large PHP document, with 15 includes. Each include is a widget. If the user checks a box on that page, that include is triggered like so:
if( in_array( 'Blog', get_field('modules') ) ) {
include 'modules/blog.php';
}
This works great! But, there is no way for the user to specify the order in which the widgets will appear. I need to develop some sort of system that will allow the user to choose the order of the modules.
Heres what I was thinking- If I can get each checkbox to spit out a specified order, I would need some sort of way within php to reposition the includes:
if( in_array( 'Blog', get_field('modules') ) ) {
$order = 2;
include 'modules/blog.php';
}
if( in_array( 'Gallery', get_field('modules') ) ) {
$order = 1;
include 'modules/gallery.php';
}
Now I would need some way to reorder these two "blocks" based on their supplied order. Can I make that happen somehow? Obviously I want to use PHP, but if not possible I could do it with jQuery. Any thoughts?
Instead of including the modules, push the list to an array which will include them in the end. Like:
$final_modules = array();
if( in_array( 'Blog', get_field('modules') ) ) {
$order = 2;
$final_modules[$order] = 'modules/blog.php';
}
if( in_array( 'Gallery', get_field('modules') ) ) {
$order = 1;
$final_modules[$order] = 'modules/gallery.php';
}
//sort based on key
ksort($final_modules);
foreach($final_modules as $order => $include_file) {
include $include_file;
}

Categories