Hot answers tagged

96

Add the following to wp-config.php: define( 'FS_METHOD', 'direct' ); Let me know how it works for you.


82

Arriving here exactly 2 years after the original question was asked, there are a few things I want to point out. (Don't ask me to point out a lot of things, ever). Proper hook To instantiate a plugin class, the proper hook should be used. There isn't a general rule for which it is, because it depends on what the class does. Using a very early hook like "...


77

WooCommerce 3.0+ you can get the order items of an order by $order = wc_get_order( $order_id ); $items = $order->get_items(); then if you loop through the items, you can get all the relevant data: foreach ( $items as $item ) { $product_name = $item->get_name(); $product_id = $item->get_product_id(); $product_variation_id = $item->...


72

Yes. When you register your post type you need to set show_in_menu to the page you would like it displayed on. Adding a custom post type as a sub-menu of Posts Here we set the "movies" post type to be included in the sub-menu under Posts. register_post_type( 'movies', array( 'labels' => array( 'name' => __( '...


70

To add our button to the TinyMCE editor we need to do several things: Add our button to the toolbar Register a TinyMCE plugin Create that TinyMCE plug-in which tells TinyMCE what to do when our button is clicked. Steps #1 and #2 In these steps we register our TinyMCE plug-in which will live inside a javascript file at 'path/to/shortcode.js' (see ...


63

Good question, there are a number of approaches and it depends on what you want to achieve. I often do; add_action( 'plugins_loaded', array( 'someClassy', 'init' )); class someClassy { public static function init() { $class = __CLASS__; new $class; } public function __construct() { //construct what you see fit here....


63

When you visit a frontend page, WordPress will query the database and if your page does not exist in the database, that query is not needed and is just a waste of resources. Luckily, WordPress offers a way to handle frontend requests in a custom way. That is done thanks to the 'do_parse_request' filter. Returning false on that hook, you will be able to ...


50

The solution I found that worked on my Mac running the built-in Apache2 was to add this to 'wp-config.php' define('FS_METHOD','direct'); Keep this to a localhost or dev setup and don't use it for production.


42

has_term should work in this case: if ( has_term( 'audio', 'product_cat' ) ) { echo 'In audio'; woocommerce_get_template_part( 'content', 'single-product' ); } elseif ( has_term( 'elektro', 'product_cat' ) ) { echo 'In elektro'; woocommerce_get_template_part( 'content', 'single-product' ); } else { echo 'some blabla'; }...


39

This is just, how I understood the idea of the WordPress File API. If it is wrong, please downvote :) Okay. If you upload a file, this file has an owner. If you upload your file with FTP, you login and the file will be owned by the FTP user. Since you have the credentials, you can alter these files through FTP. The owner can usually execute, delete, alter ...


38

Hameedullah Khan's answer will throw a PHP warning. Include this if clause to check to make sure it's an object before unsetting the response for that plugin. 'Warning: Attempt to modify property of non-object' Try this to avoid the warnings (code for the plugin file itself): // remove update notice for forked plugins function remove_update_notifications($...


38

Filters and actions are both assigned to hooks. Functions assigned to hooks are stored in global $wp_filter variable. So all you have to do is to print_r it. print_r($GLOBALS['wp_filter']); PS. add_action function makes a add_filter call. And the latter does $wp_filter[$tag][$priority][$idx]. NOTE: you can directly add this code in functions.php, and you ...


36

Products in WooCommerce are a custom post type, so this should work: $url = get_permalink( $product_id ); You can treat that $product_id as a postID (that's what it is), so you can use it with other normal WP functions, like: echo '<a href="'.get_permalink($product_id).'">'.get_the_title($product_id).'</a>';


35

Whenever you find a piece of code without clear installation instructions it is probably a plugin. The example you gave is a good one, because that is the most common case: add_action('template_redirect', 'remove_404_redirect', 1); function remove_404_redirect() { // do something } To use such a snippet, put it into a plugin: Create a new file, name it ...


35

I fixed this by going through PHPMyAdmin to the table "Options" and then the row active_plugins. I had the following stored there (formatted for readability): a:10:{ i:0;s:49:"1and1-wordpress-wizard/1and1-wordpress-wizard.php"; i:1;s:29:"acf-repeater/acf-repeater.php"; i:2;s:30:"advanced-custom-fields/acf.php"; i:3;s:45:"limit-login-...


32

Because I had cut and pasted define(’FS_METHOD’,’direct’); from the web, the incorrect quote characters were pasted into the file. should be define('FS_METHOD', 'direct'); The incorrect quote character also changed the encoding of the file somehow. If anyone has deeper knowledge of this issue please let me know in the comments.


30

I don't think get_categories() is the best option for you in this case because it returns a string with all the categories listed as anchor tags, fine for displaying, but not great for figuring out in code what the categories are. Ok, so the first thing you need to do is grab the product/post object for the current page if you don't already have it: global ...


30

Same can be done through functions.php in your (child) theme: add_filter("woocommerce_checkout_fields", "order_fields"); function order_fields($fields) { $order = array( "billing_first_name", "billing_last_name", "billing_company", "billing_address_1", "billing_address_2", "billing_postcode", ...


30

Firstly, you can't immediately achieve the goal you described with the code you show in your question. To understand why there isn't a direct approach with that code you have to take a look at what you're using - woocommerce_related_products() - to do what you have done so far. Secondly, you have to understand, because otherwise you are not able to ask a ...


28

Updated answer Cleaner and shorter version. function load_movie_template( $template ) { global $post; if ( 'movie' === $post->post_type && locate_template( array( 'single-movie.php' ) ) !== $template ) { /* * This is a 'movie' post * AND a 'single movie template' is not found on * theme or child theme ...


28

You need to create a new loop for that. Here's the code I use for displaying products from a specific category on the home page: <ul class="products"> <?php $args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'shoes', 'orderby' => 'rand' ); $loop = new WP_Query( $args ); while ( $...


28

To disable user email notification, add this in a plugin or theme: add_filter( 'send_password_change_email', '__return_false' ); FYI wp_password_change_notification() controls admin email notification when a user changes their password


27

Plugin Necessity What the necessity of plugins really boils down to is the question, "Am I satisfied that WordPress's core functionality is all that I need?" If all you want is a simple blog with some categories and a number of static pages you're set. But if you want to start integrating interactive maps, calenders with events, maybe a 3rd-party REST API, ...


27

Most of the following can be found in the Codex: apply_filters The callback functions attached to filter hook $tag are invoked by calling this function. This function can be used to create a new filter hook by simply calling this function with the name of the new hook specified using the $tag parameter. $value = apply_filters( $tag, $value, $var_1, $...


25

Overriding get_page_template() is just a quick hack. It does not allow the template to be selected from the Admin screen and the page slug is hard-coded into the plugin so the user has no way to know where the template is coming from. The preferred solution would be to follow this tutorial which allows you to register a page template in the back-end from ...


25

You can look at the docs, but you'll probably end up reading the real documentation: the source code. If you do, you'll find that: GFFormsModel::get_leads($form_id) returns a list of entries for a form (maybe you know that one already), where each item in the array is itself an array, an "Entry object" GFFormsModel::get_form_meta($form_id) returns a list ...


25

There's actually a filter that you can use, see abstract-wc-email.php, line 214: return apply_filters( 'woocommerce_email_recipient_' . $this->id, $this->recipient, $this->object ); you can put the following in your functions.php: add_filter( 'woocommerce_email_recipient_customer_completed_order', 'your_email_recipient_filter_function', 10, 2); ...


25

taken from that very same function: // prior to wordpress 4.5.0 $args = array( 'number' => $number, 'orderby' => $orderby, 'order' => $order, 'hide_empty' => $hide_empty, 'include' => $ids ); $product_categories = get_terms( 'product_cat', $args ); // since wordpress 4.5.0 $args = array( 'taxonomy' =&...


25

You could do this using the WordPress uninstall.php support: <?php if( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) exit(); global $wpdb; $wpdb->query( "DROP TABLE IF EXISTS NestoNovo" ); delete_option("my_plugin_db_version"); ?> This uninstall.php file is called when your plugin is deleted.


24

If you want to add a add media button to your admin panels: You need to use wp_enqueue_media(); add_action ( 'admin_enqueue_scripts', function () { if (is_admin ()) wp_enqueue_media (); } ); Then use this js: jQuery(document).ready(function() { var $ = jQuery; if ($('.set_custom_images').length > 0) { if ( typeof wp !== '...


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