Wordpress Theme Development - php

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

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 can I simplify this code with a loop or function?

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

Copy advanced custom field content from other page using if statement

Im trying to copy some content from another page with an if statement that has an input from advanced custom fields so I dont have to manage two pages with the same content. However the code has a bug in it which prevents the page from loading when this section starts. I guess I made a type somewhere. What could it be?
The first block contains the information from post id 4767 that i want to display on post id 4940. The second block is the information that would normally be loaded and is entered on all the individual pages.
<?php if(is_single('4940')) {
if( have_rows('partner', 4767) ):
while ( have_rows('partner', 4767) ) : the_row();
$partnerTag = get_sub_field('partner_tag', 4767);
$partnerName = get_sub_field('partner_name', 4767);
$partnerImage = get_sub_field('partner_image', 4767);
$partnerImageUrl = $partnerImage['sizes']['medium'];
$partnerLink = get_sub_field('link', 4767);
} else {
if( have_rows('partner') ):
while ( have_rows('partner') ) : the_row();
$partnerTag = get_sub_field('partner_tag');
$partnerName = get_sub_field('partner_name');
$partnerImage = get_sub_field('partner_image');
$partnerImageUrl = $partnerImage['sizes']['medium'];
$partnerLink = get_sub_field('link');
} ?>
ps. I tried both: is_single('4940') and is_single(4940)
You could just check the value of $post->ID directly.
if( $post->ID == 4940 ) { ... }
Are you within the query? is_single() and is_singular() will not work outside loop.

How can I get in_array() to work with get_option()?

I have two custom fields in my admin settings that allow me to enter two sets of valid zip codes ('woocommerce_local_delivery_zone_a' and 'woocommerce_local_delivery_zone_b').
I cannot figure out why this code isn't working:
$codes_a = get_option( 'woocommerce_local_delivery_zone_a' );
$codes_b = get_option( 'woocommerce_local_delivery_zone_b' );
if ( in_array( "55410", array( $codes_a ) )) {
echo "Codes A";
}
if ( in_array( "55410", array( $codes_b ) )) {
echo "Codes B";
}
If I echo $codes_a or $codes_b (outside of the "if" statement), the string of zip codes returns on the page. If I take that string and enter it directly where the variable is in if (in_array( "55410", array( NUMBERS HERE ))) the text I am trying to echo (Codes A or Codes B) works.
I also tried placing array( get_option( 'field_name')) directly in the if statement, and that didn't do anything either. Does in_array not work with getting admin custom fields?
If $codes_a is a string like this: 08847,032434,35545 then you need to use explode() to turn it into an array. I suspect what you really want is this:
if ( in_array( "55410", explode( ',', $codes_a ) ) {
echo "Codes A";
}

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