Firing a function AFTER redirect

I am using a function that redirects the user to a custom page, rather than wp-login in the event of errors. In my case I am using the Http Referer function to send the user back to the same page because I have the login form in a modal. How can I execute a function on the redirected page, so that the page automatically opens the modal? This is the code for the redirect in the event of an error: …
Category: Web

Restrict editing of post type to list stored in user meta

What is the right action to invoke before a custom post type is getting edited in dashboard and contains the argument the post id? The url /wp-admin/post.php?post=282&action=edit&classic-editor here is what I try $userId = get_current_user_id(); $affiliate = get_field('field_627ff399b5ef6', 'user_' . $userId); $this->currentAffiliate = $affiliate; $subPages = get_children([ 'post_parent' => $affiliate[0]->ID, 'post_type' => 'affiliate', ]); $pageIds = collect($subPages)->pluck('ID')->push($affiliate[0]->ID)->toArray(); if edit page ID is not in $pageIds redirect to the not allowed screen
Category: Web

Best possible way to get all options

I need to provide a list of all options set via plugins etc to remote calls. There is a plugin WP API Options but it hardly does the job. There are two possible ways (that I know of) to get a list of all options set in the blog/site: Use $wpdb to run a query like so: $option_names = $wpdb->get_col( "SELECT DISTINCT option_name FROM $wpdb->options WHERE option_name NOT LIKE '_transient_%'" ); Use wp_load_alloptions() Which one of these two would be …
Category: Web

Fatal error: Uncaught Error: Class 'WP_Block_Styles_Registry'

I am having some issues determining the solution to this problem below. The website was sent to our team to fix. Turned on error reporting in wp-config.php: define('WP_DEBUG', true); WordPress version: 5.3.2PHP version: 7.2 WordPress Theme: Grow Pro Fatal error: Uncaught Error: Class 'WP_Block_Styles_Registry' not found in /home/website/public_html/clientsite.com/wp-includes/script- loader.php:2902 Stack trace: #0 /home/website/public_html/clientsite.com/wp-includes/class-wp- hook.php(288): enqueue_block_styles_assets('') #1 /home/website/public_html/clientsite.com/wp-includes/class-wp- hook.php(312): WP_Hook->apply_filters(NULL, Array) #2 /home/website/public_html/clientsite.com/wp-includes/plugin.php(478): WP_Hook->do_action(Array) #3 /home/website/public_html/clientsite.com/wp-includes/script- loader.php(2856): do_action('enqueue_block_a...') #4 /home/website/public_html/clientsite.com/wp-includes/class-wp- hook.php(288): wp_common_block_scripts_and_styles('') #5 /home/website/public_html/clientsite.com/wp-includes/class-wp- hook.php(312): WP_Hook->apply_filters(NULL, Array) #6 /home/website/public_html/clientsite.com/wp-includes/plugin.php(478): WP_Hook->do_action(Array) #7 …
Category: Web

Filter wp_mail based on content type

I can current filter the content type of the wp_mail function: add_filter( 'wp_mail_content_type', function( $content_type ) { return 'text/html'; }); And I can filter the message content add_filter( 'wp_mail', function( $args ) { $args['message'] = 'Filtered text here'; return $args; }); But how can I only do the latter conditionally? I only want to filter the message when the content type == text/plain. I'm guessing there is a super-simple solution to this, but I haven't worked it out yet.
Category: Web

How to stop execution of a function via add_action hook?

I have the following functions: function test($post_id){ do_action('test_action',$post_id); echo $post_id; } add_action('test_action',function($post_id){ if ( $post_id == 2 ) //Stop test function execution } Using the function hooked to add_action, how to stop the execution of test() function without adding any code to test(). In the above example, if $post_id == 2 , the echo $post_id; code should not run in test().
Category: Web

Limiting actions only to edit.php page for specific custom post type

Excuse me if something like this has been asked before. So, I'm trying to alter the query which displays the posts of a custom post type, but on the admin side, more precisely, the edit.php page. This is my code: function my_function() { global $pagenow, $typenow; if( 'edit.php' == $pagenow && 'certain-post-type' === $typenow ) { // do something } } add_action( 'admin_init', 'my_function' ); The main problem is not altering the query itself (yet!), but targeting only the edit.php …
Category: Web

Create action running on trashed_post hook to modify post_meta value

I have a custom post type that always do math operation to its post_meta and other custom post type post_meta. For example: post-type 1 = cpt_product_order post-type 1 post_meta = cpt_pm_product_order post-type 2 = cpt_product post-type 2 post_meta = cpt_pm_product_stock There's a cpt_product with cpt_pm_product_stock = 100, frontend operation enable specific user made order by inserting new cpt_product with post_meta cpt_product_order of 10 will substract cpt_pm_product_stock to 90 100 cpt_pm_product_stock - 10 cpt_product_order = 90 cpt_pm_product_stock cancelling order will delete/trash …
Category: Web

How to add numeric slug for child page in WordPress 5.9?

I am trying to configure WordPress 5.9 to allow numeric slugs for child pages. Drawing from the 2015 answer to a similar question I took the up-to-date code of wp_unique_post_slug, removed || preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug ) and then added it as the following hook to my theme's functions.php. But for some reason, I just get a critical error: "Allowed memory size exhausted". What can I do to allow numeric slugs for child pages? add_filter( 'wp_unique_post_slug', function ( $slug, $post_ID, $post_status, …
Category: Web

change attachment custom field onChange event

The form is in post creating window. Ajax is posting values, but the values doenst renews. Where can be problem ? Ajax <script> function dynamic_Select(field, aid, value) { console.log(field, aid,value); jQuery.ajax({ type: "POST", url: ajaxurl, data:{ field: field, aid: aid, value: value, }, error: function(){alert('Error!')}, success: function(){alert(value)} }); } </script> functions.php add_action('wp_ajax_dynselect', 'update_dynamic_select'); add_filter("attachment_fields_to_save", " update_dynamic_select", null , 2); function update_dynamic_select($field, $aid, $value) { update_post_meta($aid, $field, $value); } HTML: <select aid="104" name="_image_matmenys" onchange="dynamic_Select(this.getAttribute('name'), this.getAttribute('aid') ,this.value)"> <option value="10x10">10x10</option> <option value="20x20" selected="selected">20x20</option> …
Category: Web

Creating posts with links from a txt file

I have a script which scrapes articles from the web and saves the url to a .txt file. I've created a custom plugin which on activation, loops through the urls and creates a draft post with the content being an embedded article. I'm using the following code in my plugin <?php register_activation_hook(__FILE__, 'my_plugin_activate'); function my_plugin_activate(){ my_plugin_install_site(); } function my_plugin_install_site(){ global $user_ID; $handle = fopen("listOfURLs.txt", "r"); if ($handle) { $count = 0; while (($line = fgets($handle)) !== false) { // process …
Category: Web

wordpress plugin translation not working

I have no idea what is wrong here. First I would like to share probably all required info. Plugin tree: $ tree . ├── classes │ ├── database │ │ ├── plugin-auth.php │ │ ├── plugin-user.php │ │ └── wp-user-x.php │ ├── main │ │ └── plugin.php │ └── woocommerce │ └── plugin-order.php ├── config.php ├── plugin.php ├── languages │ ├── plugin-shop-en_US.mo │ ├── plugin-shop-en_US.po │ ├── plugin-shop-pl_PL.mo │ └── plugin-shop-pl_PL.po └── templates └── register.php plugin.php contains Text Domain: plugin-shop …
Category: Web

When WP_CLI runs Wordpress from the command line, are hooks called?

Yesterday, I found out that a website I worked on as a writer (I have no admin access) had been injecting malicious Javascript code in all its pages, as described in this article by Luke Leal. According to that article, a fake Wordpress plugin musts have been installed on that website to inject the malicious code. I want to draw your attention to this section of the malicious code: // This code is defined inside a PHP class... function save_striplple_plugin() …
Category: Web

Add action hook into wp_localize_script

Is it possible to add an action hook via wp_localize_script, so I can position where I want the hook to run in HTML markup that is made in the JavaScript? So something like: $data = array ( 'ng_slicknav' => array( 'ng_slicksearch' => home_url( '/' ), 'ng_slicknav_closedsymbol' => esc_html( $options['ng_slicknav_closedsymbol'] ), 'ng_slicknav_hook' => do_action( 'myplugin_after_hook' ), ), ); // Add filter $data = apply_filters( 'ng_slicknav_slickNavVars', $data ); // Pass PHP variables to jQuery script wp_localize_script( 'slicknav-init', 'slickNavVars', $data ); wp_enqueue_script( 'slicknav-init' …
Category: Web

How to cancel the trash action inside wp_trash_post

I need to restrict the button "EMPTY TRASH" and make it delete only the posts only if the logged in user is the creator. I tried to use the wp_trash_post and i need to decide in the function if i need to continue to trash or not. How do i tell WordPress NOT to empty the trash from specific posts?
Category: Web

WP_Query retuning empty array in rest api

I recently started writing a rest api for my plugin. I need to access some custom posts however WP_Query is returning an empty array when I'm querying the db. There is the custom post type declaration. // custom post type declaration add_action('init', 'register_plugin_custom_post_type'); function register_plugin_custom_post_type() { register_post_type('my_post_type', [ 'label' => "A name", 'StackExchange exemple', 'public' => false, 'show_in_rest' => true ]); } Rest api route registration add_action('wp_rest_init', 'my_plugin_rest_init_callback'); function my_plugin_wp_rest_callback() { register_rest_route('my-rest-route', '/endpoint', [ 'method' => 'GET', 'callback' => 'rest_api_callback', …
Category: Web

Same URL for portfolio and for a page creates 404 error. Is there any filter that i can use for a child page?

What I try to achieve is to have the portfolio slug as a page slug also, and when I will assign the page as a parent to another, then that page must look for a specific page's parent ID and return the correct results. If I have the same slug in portfolio and in a page and try to create a child page, then I will get an error. Example: Portfolio slug: test/rent-a-car. Page slug: test/rent-a-car. If I create a …
Category: Web

Hook into all password resets in Wordpress and get password before hashing?

I'm syncing my Wordpress authentication system with an secondary/external authentication system and my site has at least two ways of resetting the password, including: Password reset email User account screen password reset There may be some third way I'm not recalling, as I've disallowed password resets thus far due to my inability to sync the systems. The crux of my question: How can I hook into the password reset prior to hashing so that I can simultaneously set the new …
Category: Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.