How to overwrite basic WordPress features?

This is my first time dealing with WordPress, and I have the task of extracting the main functions that have been configured, and putting them in a non-core file so that it is more convenient when updating. Here is an example of one of the methods in wp-admin/includes/template.php :

Source:

  function meta_form() { global $wpdb; $limit = (int) apply_filters( 'postmeta_form_limit', 30 ); $keys = $wpdb->get_col( " SELECT meta_key FROM $wpdb->postmeta GROUP BY meta_key HAVING meta_key NOT LIKE '\_%' ORDER BY meta_key LIMIT $limit" ); if ( $keys ) natcasesort($keys); ?> 

Individual version:

 function meta_form() { global $wpdb; if ( isset($_GET['post']) ) $post_id = (int) $_GET['post']; elseif ( isset($_POST['post_ID']) ) $post_id = (int) $_POST['post_ID']; else $post_id = 0; if ( $post_id ) { $post_ = get_post($post_id); } if ($post_->post_type == 'video_photo' ){ $limit = (int) apply_filters( 'postmeta_form_limit', 30 ); $keys = $wpdb->get_col( " SELECT meta_key FROM $wpdb->postmeta where meta_key like 'tqmcf_%' GROUP BY meta_key HAVING meta_key NOT LIKE '\_%' ORDER BY meta_key LIMIT $limit" ); }else{ $limit = (int) apply_filters( 'postmeta_form_limit', 30 ); $keys = $wpdb->get_col( " SELECT meta_key FROM $wpdb->postmeta GROUP BY meta_key HAVING meta_key NOT LIKE '\_%' ORDER BY meta_key LIMIT $limit" ); } if ( $keys ) natcasesort($keys); ?> 

Where exactly can I define my meta_form() function to make sure it overwrites the main method?

+5
source share
4 answers

In WordPress, you cannot trust the whole core. You have two ways to change WordPress Core:

The first option: plug-in functions

Some functions may be overridden. This can be done in the functions.php file or in the plugin file. You can check out the list of Pluggable Functions .

Second option: Filter and Action hooks

  • Filter hooks can be used to modify vars inside the WordPress core.
  • Action hooks can be used to trigger custom functions for some events.

Note 1: you can create your own filter and action hooks in your theme or plugin.

Note 2: you can use filter hooks to trigger a function if you do not find a convenient action hook.

If you follow WordPress coding standards, you can deeply change the behavior of WordPress.

For your display function, you should look for a post_where filter to do something like this:

 add_filter( 'posts_where' , 'my_posts_where' ); function my_posts_where( $where ) { global $post; if ($post->post_type == 'video_photo' ){ $where .= " AND meta_key like 'tqmcf_%'"; } return $where; } 

Edit 1:. The following may be more appropriate, even if this particular request is difficult to configure.

 add_filter( 'query' , 'my_meta_form_query' ); function my_meta_form_query( $query ) { $pattern = "/SELECT(?:\W*)meta_key(?:\W*)FROM (.*)(?:\W*.*?)*LIMIT(?:\W*)([0-9]*)/g"; if( preg_match($pattern, $query, $vars) ) { $postmeta = $vars[1]; $limit = $vars[2]; $query = "SELECT meta_key FROM $postmeta WHERE meta_key like 'tqmcf_%' GROUP BY meta_key HAVING meta_key NOT LIKE '\_%' ORDER BY meta_key LIMIT $limit"; } return $query; } 

You need to insert the code into the code to find the suitable hooks and how best to filter the variables. In this example, we check if the request matches the method in the meta_form() function. We retrieve existing query vases and build a new query, including the WHERE . I have not tested this piece of code, and there may be some errors, but it can give you an idea of ​​how we change the main code.

+8
source

There is no replacement for core functions unless defined in wp-includes/pluggable.php .

You can change values ​​and processes using hooks where available (you will find them mainly using the apply_filters and do_action in the main files). IMO, there are more experts and a knowledge base in WordPress Answers , but I suggest focusing on the end result, because you can connect to wpdb or postmeta , for example. In your code example, it’s not clear what you are trying to achieve, but the only available access to the filter does not seem adequate, hence another goal and a clear goal.

At the end of the day, the correct way to change the behavior of WordPress is through the plugin API .

+4
source

If you know the name of the WP function you want to replace, put something like this in the functions.php theme file:

 function my_new_function () { /* Your replacement code.... */ } remove_filter (the_content, old_wp_function); add_filter (the_content, my_new_function); 

"the_content" is the publication of all content, so you can use it, for example, to replace the Wordpress wptexturize () function (which, among other things, replaces regular quotes with "smart" quotes) with your own. [Or, if you just do not want to use the WP function, just use the remove_filter statement above.]

0
source

Why not just edit the main file and just add if (! Function_exists ...) then copy and edit the functions to the funcitons.php file.

Then, when updating, you will get errors of all functions declared twice, but it will be quite easy to go over and declare if (! Function_exists ...)

-1
source

Source: https://habr.com/ru/post/1487154/


All Articles