aissamhidoussi
Forum Replies Created
-
Forum: Developing with WordPress
In reply to: Restrict REST API access to some pages by user roleIt turned out that it was a misunderstanding from my part, in fact the above method doesn’t restrict page content, even in the front-end, it just redirect users, and the page’s content is still retrievable through other methods (search, feed, …etc)
I’ve ended up with delivering pages content programmatically through shortcodes where I can check permissions strictly, and the redirections are just for a good user experience.
Forum: Developing with WordPress
In reply to: Restrict REST API access to some pages by user roleThank you @bcworkz
I couldn’t find a good way to use the rest_pre_echo_response filter to accomplish what I need, however, I’ve searched other rest api filter and I found rest_request_before_callbacks. and I’ve tried this code:
function pgmgr_restrict_api_requests( $response, $handler, WP_REST_Request $request ) { global $current_user; $routes_require_login = null; foreach( pgmgr_get_page_ids_by_slug('csd') as $key => $val ) $routes_require_login[] = '/wp/v2/pages/' . $val; if( PGMGR_DEBUG ) { pgmgrdebug($routes_require_login,__FILE__,__LINE__,true); pgmgrdebug($request,__FILE__,__LINE__,true); } if ( $current_user instanceof WP_User && ! $current_user->exists() && in_array( $request->get_route(), $routes_require_login, true ) ) { return new WP_Error( 'rest_cannot_read', 'No permission to view this post.', array( 'status' => 401 ) ); } return $response; } add_filter( 'rest_request_before_callbacks', 'pgmgr_restrict_api_requests', 10, 3 );
It works fine when requesting wp/v2/pages/page_id, and return 401 response, but it deliver the content of page when requesting it using other methods, i.e., wp/v2/pages?slug=page_slug
Is there a method to check if some page with id is requested through rest api whatever the method, or should i check all available method to request a page through rest api and validate them to restrict the access?
Thank you very much, i’ll wait for the next update.
For now, as a temporary solution, I’ve added the wallet to menus dynamically, with a check; if primary at the end, if handheld at the 2nd position. About the icon, i couldn’t figure its css, i tried the unicode with awesomfont css and i’ve found that it’s a user profile icon, not a wallet icon.
// Add mini-wallet to primary and handheld menus function new_nav_menu_items($items, $args) { if (!function_exists('woo_wallet') || !is_user_logged_in()) { return $items; } $title = __('Current wallet balance', 'woo-wallet'); $mini_wallet = '<li id="menu-item-4661" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-4661 focus"><a style="color:#FFFFFF;" href="' . esc_url(wc_get_account_endpoint_url(get_option('woocommerce_woo_wallet_endpoint', 'woo-wallet'))) . '" title="' . $title . '"><i class="fas fa-wallet"></i> '; $mini_wallet .= woo_wallet()->wallet->get_wallet_balance($user_id); $mini_wallet .= '</a></li>'; if($args->theme_location == 'primary') { // add the mini wallet link to the end of the menu $items = $items . $mini_wallet; } else if($args->theme_location == 'handheld') { // add the mini wallet link to the second plage of the menu $items_array = array(); while ( false !== ( $item_pos = strpos ( $items, '<li', 2 ) ) ) // Add the position where the menu item is placed { $items_array[] = substr($items, 0, $item_pos); $items = substr($items, $item_pos); } $items_array[] = $items; array_splice($items_array, 1, 0, $mini_wallet); // insert custom item after 1th item one $items = implode('', $items_array); } return $items; } add_filter( 'wp_nav_menu_items', 'new_nav_menu_items', 10, 2);
I appreciate if you could add options to control mini-wallet’s position in different available menus in the next update/release.
Thank you for your help.
Thank you, waiting for your post.
For now, is there any plugin to control user’s registration and Super Socializer is compatible with it?