What is the Correct way to pass parameters in function

The following is problematic in the OP’s code snippet: Missing shortcode_atts() for default attribute handling. Incorrectly defining the shortcode’s input argument, use instead function create_my_shortcode( $attr = [], $content = null ){ … } Not escaping user input, check e.g. wpdb::prepare(). We want to avoid possible SQL injections. Uses hardcoded table prefix, wp_, instead of … Read more

Get custom fields from a custom post type with a shortcode

Get the Attribute value in the shortcode and get the custom field values: function product_func($atts) { $post_id = $atts[‘code’]; $key = “my_custom_field_key”;//for 1 field, you can do this 6 times for the 6 values $custom_value = get_post_meta($post_id, $key, true); return $custom_value; } add_shortcode(‘product’, ‘product_func’); if you want to debug the post meta field values use … Read more

Shortcode is running in page editor

Thanks to @mmm I figured it out. What I am doing now is adding the form in between php tags. EXAMPLE of what I was doing ?> <input type=”checkbox” name=”overwrite” value=”true”>Overwrite?<br> <?php What I need to be doing is adding the content that I want to add to the page into a string that is … Read more

create shortcode to list users with specific meta key value

Try this: function thebroker_agents() { global $current_user; $bmail = $current_user->user_email; $user_query = new WP_User_Query( array( ‘meta_key’ => ‘broker_email’, ‘meta_value’ => $bmail, ‘fields’ => ‘all’ ) ); $users = $user_query->get_results(); if (!empty($users)) { $results=”<ul>”; foreach ($users as $user){ $results=”<li>” . $user->display_name . ‘</li>’; } $results=”</ul>”; } else { $results=”No users found”; } wp_reset_postdata(); return $results; } … Read more

Query how many items to show in shortcode

add_shortcode( ‘type_portfolio’, function( $atts, $content = null ){ $atts = shortcode_atts( array( ‘column’ => ‘3’, ‘category’ => ‘0’, ‘ppp’ => -1 ), $atts); extract($atts); $args = array( ‘posts_per_page’ => $ppp, ‘post_type’ => ‘fen_portfolio’ ); if( $category > ‘0’ ){ $args[‘tax_query’] = array( array( ‘posts_per_page’ => -1, ‘taxonomy’ => ‘cat_portfolio’, ‘field’ => ‘term_id’, ‘terms’ => $category … Read more

Shortcode “post_per_page” not working

You have the posts_per_page attribute in your shortcode, but you’re not actually calling it in your WP_Query. Your’re doing a few unnecessary things in your code, but for now, the issue at hand is that you need to add: if( !empty( $posts_per_page )) { $query .= ‘&posts_per_page=”.$posts_per_page; } Before you call: $wp_query->query($query); As it is … Read more