Is it possible to send a get request within a callback function responsible for rendering the option interface of a setting field?

im trying to display index numbers of posts of a public facebook page along with multiple checkboxes in a custom admin menu so that the user can choose what posts he wants to display.when i use a json array similar to the response of a get request; it works.(These codes are placed in functions.php).

function fb_feed_menu() {
add_theme_page(
‘Fb feed’, // The title to be displayed in the browser window for this page.
‘Fb feed’, // The text to be displayed for this menu item
‘administrator’, // Which type of users can see this menu item
‘fb_feed_options’, // The unique ID – that is, the slug – for this menu item
‘fb_feed_display’ // The name of the function to call when rendering this menu’s page
);
};
add_action( ‘admin_menu’, ‘fb_feed_menu’ );

function fb_feed_display() {
?>
<div class=”wrap”>

<form method=”post” action=”options.php”>
<?php settings_fields( ‘fb_feed_display_options’ ); ?>
<?php do_settings_sections( ‘fb_feed_display_options’ ); ?>
<?php submit_button(); ?>
</form>

</div><!– /.wrap –>

<?}

function initialize_fb_feed_options() {
// If the options don’t exist, create them.
if( false == $optons = get_option( ‘fb_feed_display_options’ ) ) {
$options = array(“post_id_feed”=>[]);
add_option( ‘fb_feed_display_options’, $options);
};

add_settings_section(
‘general_settings_section’, // ID used to identify this section and with which to register options
‘Display Options’, // Title to be displayed on the administration page
‘fb_feed_options_callback’, // Callback used to render the description of the section
‘fb_feed_display_options’ // Page on which to add this section of options
);

add_settings_field(
‘post_id_feed’, // ID used to identify the field throughout the theme
‘Post id feed’, // The label to the left of the option interface element
‘fb_post_id_callback’, // The name of the function responsible for rendering the option interface
‘fb_feed_display_options’, // The page on which this option will be displayed
‘general_settings_section’ // The name of the section to which this field belong
);
// Finally, we register the fields with WordPress
register_setting(
‘fb_feed_display_options’,
‘fb_feed_display_options’
);

}

add_action(‘admin_init’, ‘initialize_fb_feed_options’);

function fb_feed_options_callback() {
echo ‘<p>fill this</p>’;
}

function fb_post_id_callback() {

$options = get_option(‘fb_feed_display_options’);
$url = ‘https://graph.facebook.com/v2.12/’. ‘saifa.uoc.science’ .’/feed?access_token=#mytocken’;
$response = wp_remote_get( $url );
$json = json_decode( $response[‘body’] );

foreach ( $json[‘data’] as $post) { ?>
<label>
<input type=”checkbox” value=”<?echo $post[‘id’] ?>” name=”fb_feed_display_options[post_id_feed][]” <?php echo (in_array($post[‘id’],get_option(‘fb_feed_display_options[post_id_feed]’)) ? ‘checked = checked’:” “) ; ?> />
<?php echo $post[‘id’]; ?>
</label>

<?php };

}

Read more here:: Is it possible to send a get request within a callback function responsible for rendering the option interface of a setting field?

Leave a Reply

Your email address will not be published. Required fields are marked *