New answers tagged

0

The first PHP file that WordPress loads and doesn't overwrite on update is wp-config.php. At this point, WordPress is not loaded. So you can put your functions at the beginning of that file.


1

init is not the first action to run on a WordPress install. Here is a basic rundown of a typical stack order. https://codex.wordpress.org/Plugin_API/Action_Reference Try hooking into `muplugins_loaded'. I don't believe it is conditional, so it should run every time. https://codex.wordpress.org/Plugin_API/Action_Reference/muplugins_loaded <?php ...


1

Try : add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' ); function custom_override_checkout_fields( $fields ) { if(is_user_logged_in()){ unset($fields['billing']['billing_email']); } return $fields; } Source : https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/


1

I like the idea of running this through a schedule event in WordPress, https://codex.wordpress.org/Function_Reference/wp_schedule_event Here is a snippet that hopefully will get you there: <?php if(!wp_next_scheduled('daily_plugin_check')){ wp_schedule_event( time(), 'daily', 'daily_plugin_check' ); } add_action( 'daily_plugin_check', '...


0

You can mange this by adding the following to your functions.php file. Checking todays timestamp for the days you want the plugin active - on the off days the plugin is disabled. $timestamp = time(); // Timestamp $day = date( 'D', $timestamp ); // Get day from timestamp $active = array( 'Mon', 'Wed', 'Fri', 'Sat' ); // Days plugin to be active if (...


0

What about using WordPress Options API to store a flag whether it is the first switch or not: https://codex.wordpress.org/Options_API <?php add_action('after_switch_theme', 'setup_theme_options'); function setup_theme_options () { if(get_option('first_theme_activation') === false){ // Set a flag if the theme activation happened ...


0

php.net defines this as (PHP 4, PHP 5, PHP 7) function_exists — Return TRUE if the given function has been defined I have to agree with Michelle in usage in that you [sh]ould only use the check for something similar to type cast checking; so you would be looking for if a function existed (on that page or from anything called on that page/file reference) ...


0

There is a filter, wp_audio_extensions, that should allow you to add m3u8 to the whitelist of allowed file extensions. Add this code to a child theme's functions.php file, or using a plugin like Code Snippets: function wpse_341199_allow_m3u8_extension( $exts ) { $exts[] = 'm3u8'; return $exts; } add_filter( 'wp_audio_extensions', '...


1

You have to replace $post_id with get_the_id(); function status_alerts($query) { //start function global $post; // set the global $args = array( // all posts in the status post format 'posts_per_page' => -1, 'taxonomy' => 'post_format', 'field' => 'slug', 'terms' => array( 'post-format-status' ), 'operator'=> 'IN' ...


1

What you're trying isn't possible with WP_Query. The reason is that because you have only stored the duration, it's not possible to tell whether a post has expired until you know the publication date and the duration time and add them together. That's what this does: $expire = get_field( 'status_time_duration' ) + get_the_time( 'U' ); That code relies in ...


0

There's no permalink property of a product. Permalinks are not stored in the database, because they're generated based on a number of external factors. Therefore you need to use a function to retrieve the permalink. In WooCommerce the best way to do this is with: function fpd_custom_order_item_values($_product) { $url = $_product->get_permalink(); ...


-1

add this code in your search.php <?php $show_pagination = true; if($wp_query->post_count < 0) { $show_pagination = false; } ?> <?php if($show_pagination){ ?> <!-- The pagination component --> <div class="container"> <div class="row mt-3"> <?php ...


0

You can use somthing like this: function my_custom_shortcode( $atts ) { // Attributes $atts = shortcode_atts( array( 'word' => 'example', // the word which will be replaced 'part' => '1' // for identifing the different texts ), $atts, 'textblock' ); // only if a word exists ...


1

You can use is_front_page() function in your code. add_action('woocommerce_after_shop_loop_item_title', 'description_in_shop_loop_item', 3 ); function description_in_shop_loop_item() { global $product; if( is_front_page()) { // HERE define the number of words $limit = 10; $description = $product->get_description(); //...


0

The code that you have shared points towards wp-vcd malware in your WordPress website. The main symptom of wp-vcd malware are spam popups, creates Spam URLs on the website. Some variants of the malicious codes have been seen to modify core WordPress files and also add new files in the /wp-includes directory. The malware creates a backdoor which allows ...


0

Maybe your hook called too early. And after yours, this connection time changes elsewhere. Try to increase priority like that: add_filter( 'auth_cookie_expiration', 'keep_me_logged_in', 99, 3); function keep_me_logged_in( $expiration, $user_id, $remember ) {/*your code*/}


0

Tested with Wordpress 5.0.4 The first problem to solve it's the post_name value for pages that would share the same slug (e.g. about-me). The simpler solution I found is to add the author nicename (beware: in my solution it should not contain a hyphen!) as the first part of the slug: about-me page post_name for john: john-about-me about-me page post_name ...


2

If it's just a list of numbers, I'd suggest just saving them in a text file, with a number on each line. 110019 111222 112233 Then when you need the file, read its contents with PHP, and use preg_split() to turn it into an array: $file_path = plugin_dir_path( 'postcodes.txt', __FILE__ ); // Or wherever you've placed it. $file_contents = ...


1

My include has a handful of custom hooks which handle enqueuing, dequeuing and inserting custom HTML. I'm still not 100% certain what you mean, but if this code is normal WordPress code, with functions and add_action(), and you only want those hooks to run on these events pages, then the proper way to do this would be to include that file into your theme's ...


0

I haven't tried it but this seems to answer the your criteria https://www.quora.com/In-WordPress-how-do-I-add-a-widget-to-a-menu


2

Say that get_author_posts_url(1) return the following URL https://example.com/author/admin/ Passing the $author_nicename will change this. So get_author_posts_url(1, 'foo') will result in https://example.com/author/foo/ You can also check this in the source code of get_author_posts_url(). So what does passing the second argument do? It changes the ...


1

Please check below code : add_action( 'woocommerce_single_product_summary','content_after_addtocart_button' ); function content_after_addtocart_button() { global $product; $product_title = $product->get_name(); $product_price = $product->get_price(); echo '<div class="content-section"> <a href="mailto:riccardoairone@...


3

You should be able to change create_function( '$a', "return null;" ) to Anonymous Function (aka Closure) : From : add_theme_page( $this->args['page_slug'], __('Dev Mode Info', 'nhp-opts'), __('Dev Mode Info', 'nhp-opts'), $this->args['page_cap'], $this->args['page_slug'].'&tab=...


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 ...


0

This answer assumes you are using WooCommerce version 3.0 or newer. Below is your function modified to use the new CRUD objects in WooCommerce. This is the way to edit products and add data to products in the newer versions of WooCommerce. We use wc_get_products() to get an array of product objects, this function takes various parameters, for all the ...


0

Here is a solution to upload a copy of the image saved as a URL in a custom field into a local directory on your site. This will not add the image to the media library and is meant as more of a caching mechanism. You should always be sure that you have permission to use images from other sources before you use them. Only use this code for good! function ...


1

has_category() only tells you if a specific post has a given category (or any category, if none is provided). You can tell it which post to check by passing the post ID as the second argument. When used inside the loop however, you can omit the post ID and it will check the current post. The problem is that if it's used outside the loop, then the 'current ...


0

As long as has_category is used within the loop then it should work when used within index.php, archive.php, etc. You will likely run into issues if it is used outside of the loop on those templates.


0

You can create a custom shortcode wrapper that will execute the shortcode with the desired attributes, so you would use [jobs-custom] in the page: add_shortcode('jobs-custom', 'custom_jobs_shortcode'); function custom_jobs_shortcode($atts) { if (is_user_logged_in()) { $per_page = get_user_meta(get_current_user_id(), 'jobs-per-page', true); }...


0

The reason you're not finding a quick fix is that there isn't a consistent way to hide header and footer content across all templates, across all themes. Also, even builders like Elementor require the ability to enqueue their own CSS and JS. If you completely remove the header and footer, you will almost always also be completely removing the necessary hooks,...


1

%20 is how spaces are represented in URLs/links. It is not present in the user_login property. The browser is adding it because you've put in in an href attribute. How are you creating this artist CPT? If you're just creating it with the artist name as the title, then the slug isn't going to necessarily be the username. It will be sanitised into a valid ...


2

It is bad practice to end a PHP only file with the PHP closing tag (?>) as it may lead to a new line or any other character being output before the headers or other functions and thus creating problems like the one you mention. Whenever you create/edit a PHP file, don't close it, if you have to include HTML, close and open the PHP tags again. https://php....


1

This code works with WordPress 5.2 and is correct. You now need to go to your Profile and select it by going to Users > Your Profile > Admin Color Scheme select the scheme and save. Edit: Adding updated code for your colors CSS file since you're using a child theme: function additional_admin_color_schemes() { wp_admin_css_color( 'pro', __( 'Pro' ), ...


1

<?php header("HTTP/1.1 301 Moved Permanently"); header("Location: https://{$_SERVER['SERVER_NAME']}/subcategory{$_SERVER['REQUEST_URI']}"); exit(); ?> The above code was added directly to themes 404.php and everything worked as expected


1

I believe the most direct way of customizing WordPress feeds is to hook into them directly with the following filter. Edit: as we talked about, let's just edit the content to tear out any function edit_your_feed_content($content) { $content = preg_replace("/(<figure.*?[^>]*>)(.*?)(<\/figure>)/i", "", $content); return $content; } ...


0

you could try to use this: add_filter('body_class', 'custom_body_class'); function custom_body_class($classes) { global $post; if ($post->ID == 346) { $classes[] = 'services'; } return $classes; } This can be used anywhere as its pulling the global variable first.


1

In order to make this work as you expect, you need to add the following lines in your constructor (depending on the requirements you have) add_action( 'wp_ajax_myfunction', array($this, 'myfunction') ); add_action( 'wp_ajax_nopriv_myfunction', array($this, 'myfunction') ); Note that wp_ajax_nopriv_myfunction executes for users that are not logged in. The ...


2

You can add your one... /* * Function creates post duplicate as a draft and redirects then to the edit post screen */ function rd_duplicate_post_as_draft(){ global $wpdb; if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($_REQUEST['action']) && 'rd_duplicate_post_as_draft' == $_REQUEST['action'] ) ) ) { wp_die('No ...


0

By the looks of it, there's a plugin to achieve exactly this, so save you rolling your own for it. The plugin is: https://wordpress.org/plugins/permalinks-customizer/ I installed it to test that it does what you're looking for. Here's a screenshot that shows what you'd do:


0

Mahmod is right, and that last link Mahmod posted (https://www.hostinger.com/tutorials/how-to-duplicate-wordpress-page-post) is the one I used as the basis of writing my own post/page duplication functions in my functions.php files. Back up your site, use the last suggestions from that link (#4) and you can create your own post duplication and page ...


-1

No, there isnt built in function but there are a plenty of plugins that easily can do what you need as those below: https://wordpress.org/plugins/duplicate-post/ https://www.wpbeginner.com/plugins/how-to-duplicate-a-wordpress-page-or-post-with-a-single-click/ https://www.hostinger.com/tutorials/how-to-duplicate-wordpress-page-post


0

What you're looking to accomplish isn't really pagination. That threw me off initially. You're building a navigation that will be site-wide. If that's the case, you can either capture the ID from the url (this is not very performant) or add the ID to the markup. This example shows you how to add the ID as a data attribute on the menu's anchor tag: ...


0

<?php $homePosts = new WP_Query(array( 'posts_per_page' => 5 )); $count = 1; while ($homePosts->have_posts()) { $homePosts->the_post(); if ( $count % 2 == 0 ) { $class = 'right'; } else { $class = 'left'; } ?> <section class="align-<?php echo $class; ?>" > <div ...


0

There is probably some CSS you can add via Additional CSS (in Theme CUstomization) that you could use. Use the Inspector tool of your browser (F12) to see the CSS 'class' element used in the content. If the class is called 'the_content', then add this to your Additional CSS: .the_content{text-align:center !important;} You might need this, if the content is ...


0

In single.php you can wrap your featured image in a link tag with a conditional to check if the user is logged in. if ( is_user_logged_in() ) { echo '<a href="https://google.com/">'; } the_post_thumbnail(); if ( is_user_logged_in() ) { echo '</a>'; }


0

Try reinstalling WordPress from scratch. Replace everything except your wp-config.php file, .htaccess file, nginx.conf file, and wp-contents/ directory. This error is calling a missing function that's a part of WordPress core since 4.0.0.


-2

Have you tried jQuery Migrate? It provides backwards compatibility to deprecated jQuery functions.


0

instead of using get_template_directory_uri().'/assets/js/pre.js','1.0.0' try using get_theme_file_uri('/assets/js/pre.js','1.0.0') It worked for me, for some reason when use get_template_directory_uri() you will get a 403 error


0

The correct way is to use WordPress Cron and schedule your event. Also you should consider adding real cron job as outlined here for better precision. 1.) I modified your draft_the_post function to support parameter. So now we can specify which post and also i updated the portion that checking the time. 2.) dg_cron_schedule_delete_posts will schedule the ...


1

There is an error in the solution provided by @adedoy, since $slug is never defined. This worked for me: function include_file($atts) { $atts = shortcode_atts( array( 'path' => 'NULL', ), $atts, 'include' ); ob_start(); get_template_part($atts['path']); return ob_get_clean(); } add_shortcode('include', 'include_file');


Top 50 recent answers are included