IF condition based on wp_remote_get output

Following would be the revised example code which you may want to try. function display_api_response() { $api_url = “https://randletter2020.herokuapp.com”; $response = wp_remote_get($api_url); if ( 200 === wp_remote_retrieve_response_code($response) ) { $body = wp_remote_retrieve_body($response); if ( ‘a’ === $body ) { echo ‘A wins’; }else { // Do something else. } } } add_action( ‘init’, ‘display_api_response’ ); … Read more

Show form per shortcode

Use add_shortcode in your plugin or theme or, if you don’t want to add a plugin or modify the theme, use Code Snippets plugin. Your code will look like this: add_shortcode( “your-awesome-unique-shortcode”, “wpse376442_your_awesome_shortcode_callback”); function wpse376442_your_awesome_shortcode_callback($atts, $content, $shortcode_tag) { $user_id = get_current_user_id(); $balance = mycred_get_users_balance( $user_id ); $echo = “Current Balance: “.$balance.”<br><br>”; if ($_POST && $_SERVER[“REQUEST_METHOD”] … Read more

WordPress shortcode returns the data before

Change form.php <form action=”<?= $_SERVER[“REQUEST_URI”]; ?>” method=”post”> <input type=”hidden” name=”form_submit_nonce” value=”<?php echo wp_create_nonce(‘form-submit-nonce’); ?>”/> <label for=”id”>ID</label> <input id=”id” name=”id” type=”text” value=”” required /> <input type=”submit” name=”submitid” value=”Submit”> </form> Change plugin file function form_shortcode(){ if ( isset( $_POST[“submitid”] ) && isset($_POST[‘form_submit_nonce’]) && wp_verify_nonce($_POST[‘form_submit_nonce’], ‘form-submit-nonce’) ) { echo “<p>OK</p>”; } else { include “form.php”; } } add_shortcode( … Read more

Adding author option to [products] woocommerce shortcode

Try this: function htdat_shortcode_atts_products( $out, $pairs, $atts, $shortcode ) { if ( isset ( $atts[‘author’] ) ) $out[‘author’] = $atts[‘author’]; return $out; } add_filter(‘shortcode_atts_products’, ‘htdat_shortcode_atts_products’, 10, 4); function htdat_woocommerce_shortcode_products_query( $query_args, $attributes ) { if ( isset( $attributes[‘author’] ) ) $query_args[‘author’] = $attributes[‘author’]; return $query_args; } add_filter( ‘woocommerce_shortcode_products_query’, ‘htdat_woocommerce_shortcode_products_query’, 10, 2 );

How to make my shortcode load scripts and styles, from within the plugin?

plugins_url expects the second parameter to be the main plugin file, which is why __FILE__ is used in examples, it’s assuming the code is in that file, but yours is not. To fix this, you will need to retrieve __FILE__ in your main plugin file, store it, and pass it to your shortcode objects for … Read more