Obtain wordpress user role

I wanted the logged in user to get one form from other application only if he/she is Administrator and here is how I checked it. You can check for any role (even new role made through custom user role). <? require($_SERVER[‘DOCUMENT_ROOT’].’/wp-load.php’); //to load from any folder $current_user = wp_get_current_user(); $user_info = get_userdata($current_user->ID); if (in_array(“administrator”, $user_info->roles)) … Read more

Pass product object to javscript

Just change wp_localize_script( ‘load_product_info’ , ‘products’ , $gg_products); with (EDITED ->wp_localize_script() accepts an array as 3rd par.) wp_localize_script( ‘load_product_info’ , ‘products’ , array(‘current_products’=>json_encode($gg_products ) ) ); transforming the php object into a json string and building an array . Then in the js file convert the string into a js object with JSON.parse function load_product_info() … Read more

Modal opens all post IDs

So your script is doing exactly what you have told it to do. Here you are passing the element to the function, but you never use it, instead you just query all elements with a class and add another class. function moreinfoModal(field) { console.log(field.id); $(‘.moreinfo-modal’).toggleClass(‘open’); } What you should do is only add the class … Read more

How do if all posts has this category ID then do this

You can use get_category( $id ), the object it returns includes a count of posts contained in this category. To build on your example, this would be: $my_category = get_category( ’25’ ); if ( 1 <= $my_category->category_count ) { echo “OK”; } else { echo “NO OK”; }

Set user status to absent on WordPress

You are going to have to add some Javascript to the page that will check for a timeout. Remember that PHP is executed on the server, and the servers sends HTML to the visitor’s browser. One the page is generated, the server doesn’t do anything until the next request comes from that visitor (or anyone … Read more

WooCommerce multiple custom fields code

Not in stackoverflow nor stackexchange can`t answer my question. I dont know maybe they just can edit my writing dictation!. Anyway after searching in php courses my code is this now and works correctly: <?php $custom_field_type = get_post_meta($post->ID, ‘_custom_field_type’, true); $custom_field_type = array_filter($custom_field_type, ‘strlen’); if (count($custom_field_type)) { echo ‘<div id=”vijegiha”>’; echo ‘<h3 id=”vijegih3″>ویژگی های کلیدی</h3>’; … Read more

Advertisement in Loop Posts wordpress

Use WP_Query and then loop the results using break when you want to stop to put the ad. $args = [ ‘posts_per_page’ => 5 ]; $posts = new WP_Query($args); Then loop 2 <section class=”posts”> <?php $cont = 0; ?> <?php while( $posts->have_posts() ) : $posts->the_post(); $cont++; $id = get_the_ID(); //Show your post here if($cont >= … Read more