14

WordPress is old. In fact, it is older than PHP7, in which PHP introduced random_int(). WP wanted/needed this functionality before, so another method was implemented. how does the PHP interpreter understand which of the two functions I'm calling? Good question. The interpreter doesn't understand this. And hence, if you had PHP7 and would define this ...


13

esc_html() escapes a string so that it is not parsed as HTML. Characters like < are converted to &lt;, for example. This will look the same to the reader, but it means that if the value being output is <script> then it won't be interpreted by the browser as an actual script tag. Use this function whenever the value being output should not ...


7

There's a whitelist blocks removal example from the Gutenberg Handbook: var allowedBlocks = [ 'core/paragraph', 'core/image', 'core/html', 'core/freeform' ]; wp.blocks.getBlockTypes().forEach( function( blockType ) { if ( allowedBlocks.indexOf( blockType.name ) === -1 ) { wp.blocks.unregisterBlockType( blockType.name ); } } )...


6

Not specifically related to the asker, but I'm sure that most of the 18k people who came to this question were looking for this: To get WordPress admin email simply do: $admin_email = get_option('admin_email');


5

You should never post anything to plugins files directly. It's almost always a security flaw and it prevents site owner from hardening the site properly (in perfect situation no requests to PHP files inside wp-content should be necessary at all) Good practice is that you use admin_post actions... (similar to admin_ajax). So your form should look like so: &...


5

AFAIK there is only one way to remove blocks from Gutenberg - you have to use allowed_block_types filter. Unfortunately Gutenberg developers are not very familiar with WordPress hooks and filters, so they created a little monster with this one. So instead of passing a list of core blocks in there, they pass true to enable all blocks. That way you're unable ...


4

You can't necessarily replace entire arbitrary files with a child theme. WordPress will automatically look in the child theme for replacements for the templates in the template hierarchy, as well as a couple of additional files like searchform.php or comments.php, but any other files that the parent theme loads are only replaceable in a child theme if the ...


4

esc_html would be used inside of html for example between a <p> tag <p><?php echo esc_html( $some_variable ); ?></p> esc_attr would be used for escaping attribute values on html tags like so: <p my-attribute="<?php echo esc_attr( $some_variable ); ?>"></p> applying _e to the end is for using it with text domains ...


4

Functions and includes (itself) don't affect the site performance much. But... The code inside a function can affect site speed. It all depends what exactly that function does. For example function esc_attr won't affect site speed, because its code is simple and will run fast. On the other hand, functions like get_posts can take a lot of time to execute, ...


4

This is incorrect: <?php bloginfo; ?> bloginfo() is a function, so you need to use parentheses (): <?php bloginfo(); ?>


4

It's not as hard to solve as one might think... If only all plugins/themes respect best practices. If they do, then all links to admin-ajax.php are generated with admin_url function. And this function has a hook inside, so we can modify the url it returns: // This will change the url for admin-ajax.php to /ajax/ function modify_adminy_url_for_ajax( $url, $...


4

There is an inbuilt function for returning null: __return_null. So you can replace create_function('$a', 'return null;'); with just '__return_null' (note the quotes) as it seems $a is not used anyway. Or you can use an anonymous function as the argument directly: function($a) {return null;} (no quotes). Either way since the argument is expecting a ...


3

It duplicate because when you run the wp_update_post(), it will use the wp_insert_post() function and the action save_post will run again. please use the filter wp_insert_post_data to filter the value before save. https://codex.wordpress.org/Plugin_API/Filter_Reference/wp_insert_post_data Example: function wpse309780_filter_post_data($data , $postarr) { ...


3

Levi Dulstein has a lot of righ, but in my opinion Front-end-pm uses two hooks to send messages: fep_save_message (from back-end) fep_action_message_after_send (from front-end) Before sending a email few things are checked, like publishig or sending status (includes/class-fep-emails.php): function save_send_email( $postid, $post ) { if ( ! $post ...


3

I ran into this today. It's a bug. I filed a bug report here: https://core.trac.wordpress.org/ticket/46797 The problem is this line of code: $qv['name'] = trim( $qv['name'] ); Source: https://github.com/WordPress/wordpress-develop/blob/a0ca5afd8977b5a3857084d9cb1bd345166e2f21/src/wp-includes/class-wp-query.php#L764 A (malicious) user sends a request ...


3

The template_include hook allows you to change which template file is going to be used. In it, you can conditionally check if it's an author page requested, then use get_query_var() to get the requested author. You then check the authors role and see if it's what you don't want, and use a different template if so. Below is untested, but something like: ...


3

Function "works" once because before first call of is_service_class(), function is_personal not exists and whole this block is executed if ( ! function_exists( 'is_personal' ) ) { // declaration and use of function } In next call of is_service_class() is_personal is declared and the entire block is skipped. Inside the condition if ( !function_exists( '...


3

Closing PHP tag and then opening in the next line: Whenever there is something (even space, tab, newline etc.) after a closing PHP tag ?> or before an opening PHP tag <?php, PHP engine considers it as output. So the way your code is, it'll generate some unnecessary output, that often throws error or warning like: Header already sent ... So you ...


3

The admin_url() function retrieves the URL to the admin area for your current site. You needn't give the blog id for your current site. You can use this function if you aren't running WordPress multisite. And use the get_admin_url() function to get a specific site URL in the multisite admin.


3

You can use get_ancestors() to check if the post have an ancestor parent id or grandparent id etc.. But you would have to check every post for this... function rt_before_after($content) { if(is_page() || is_single()) { global $post; $ancestors = get_ancestors( $post->ID, 'page' ); if ( in_array( 27, $ancestors ) || $post->ID == '...


3

Both functions check if the function corporate_plus_footer() exists. If not, one is defined. Without going deeper into hooks and filters, you can simply declare your own corporate_plus_footer() and the other won't get loaded. Add this to your functions.php function corporate_plus_footer() { echo 'it works'; } No additional add_action() or similar. ...


3

The reusable block is registered with the core/block name. I tried to add it to the allowed blocks in the allowed_block_types filter, here's an example: add_filter( 'allowed_block_types', 'wpse324908_allowed_block_types', 10, 2 ); function wpse324908_allowed_block_types( $allowed_blocks, $post ) { $allowed_blocks = array( 'core/block', // &...


3

The end-callback of wp_list_comments() One way is to use the end-callback argument in: wp_list_comments( [ 'end-callback' => 'wpse_comments_ads_injection', ... other arguments ... ], $comments ); in addtion to your other wp_list_comments arguments, by defining: function wpse_comments_ads_injection( $comment, $args, $depth ) { static $...


3

After the post save (using save_post hook) you check if the saved post has your "unique" category, and if so remove the category from the other posts, keeping the one you just saved. add_action( 'save_post', 'set_post_unique_category', 10,3 ); function set_post_unique_category( $post_id, $savedPost, $update ) { // Only set for post_type = post if (...


3

I'm not entirely sure what you want to do exactly. But basically you just create one instance and then add functions like public static function FunctionName() to be used anywhere you want after the plugin got activated. To fire your hooks simply use the public function __construct(). Taking the Sample Plugin from below, the FooBar method can be called in ...


3

One way is to use static methods from class itself, as @leymannx explained in his answer. I prefer a little bit different approach, which is a little bit nicer/cleaner (but it's only my opinion, so feel free to argue with that statement). I do it like so: If some function should be available for use in templates, then I register is as a template tag (so I ...


3

You're trying to call this custom function without any parameters: lb_acf_profile_avatar() But it's not meant to work like that. It's not a function that you should call. It's a filter callback and it has to get 5 parameters: $avatar, $id_or_email, $size, $default, $alt So yes - you will get errors if you won't pass them. But, as I've already said, this ...


3

As discussed in comments you actually just want the URL. Here's your code modified to be a get_avatar_url hook instead: add_filter('get_avatar_url', 'lb_acf_profile_avatar_url', 10, 5); function lb_acf_profile_avatar_url($url, $id_or_email, $args) { $user = ''; // Get user by id or email if (is_numeric($id_or_email)) { $id = (int) $...


3

Are you just looking for a simple if then statement? Assuming that your fields are set up properly, then this code should work: <?php $user_ID = get_current_user_id(); $label = 'user_' . $user_ID; if (get_field('Information', $label)) { //there is data; } else { echo 'Sorry no information here'; } ?>


3

The function that generates the new user notification is wp_new_user_notification(). It is called by another (similarly named) function wp_new_user_notifications() which is triggered by the action hook register_new_user in the register_new_user() function. If you follow that (or even if you don't), all you need to do is remove the wp_new_user_notifications(...


Only top voted, non community-wiki answers of a minimum length are eligible