Similarly to this topic I need to extract some page IDs from user input (a theme options admin page), store them in a variable, and then filter using the is_page()
filter. The input consists of numerical IDs separated by newlines, although if I get this working I'll refactor to allow page slugs too.
Based on the answer to the linked topic, I have the following, But it still does not work. The variable spits out a proper array of IDs, but is_page()
doesn't filter based on the array.
// Wordpress my_conditional_load stuff code above
function my_option_pages() {
$pages = get_option(esc_textarea('my_conditional_load', ''));
return $pages; // reusable variable; used in form and also below
}
function my_option_result() {
$pages = my_option_pages();
$page = array_map(trim, explode(PHP_EOL, $pages)); // create array; split string at newline
$result = implode( ', ', $page ); // reassemble as comma delimited list
$result = rtrim($result); // get rid of extra whitespace
return $result; // string; resusable variable used below end elsewhere. Must be a string for other uses.
}
add_filter( 'the_content', 'test_the_variable' );
function test_the_variable( $content ) {
$result = my_option_result();
$result = explode(', ', $result); // recreate the array
if ( is_page( array( $result ) ) ) {
echo '<span class="foo hide">Array $result = ';
print_r($result);
echo '</span>';
}
else {
echo '<span class="bar hide">Array $result is EMPTY! But we found this: ';
print_r($result);
echo '</span>';
}
return $content;
}
This page is a working example. It's page ID 9, which is in the array. Open DevTools and find the string bar hide
in the page source. You can see the array:
<span class="bar hide">Array $result is EMPTY! But we found this: Array
(
[0] => 9
[1] => 6604
[2] => 6509
[3] => 7323
)
</span>
So what's wrong?