I have a wordpress multisite with one main and four child website. i have done plugin for share woocommerce product to childsite. now child site can add or update mainsite product from child site. but when displaying in front end the media path is wrong. i want use all child site product image path to same path. how it possible ?
In woocommerce plugin and in file class-wc-admin-post-types.php
woocomerce override the WordPress upload filter through the filter 'upload_dir'
add_filter( 'upload_dir', array( $this, 'upload_dir' ) );
public function upload_dir( $pathdata ) {
// Change upload dir for downloadable files
if ( isset( $_POST['type'] ) && 'downloadable_product' == $_POST['type'] ) {
if ( empty( $pathdata['subdir'] ) ) {
$pathdata['path'] = $pathdata['path'] . '/woocommerce_uploads';
$pathdata['url'] = $pathdata['url']. '/woocommerce_uploads';
$pathdata['subdir'] = '/woocommerce_uploads';
} else {
$new_subdir = '/woocommerce_uploads' . $pathdata['subdir'];
$pathdata['path'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['path'] );
$pathdata['url'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['url'] );
$pathdata['subdir'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['subdir'] );
}
}
return $pathdata;
}
so if you want to override it, you can create filter to this 'upload_dir' with higher priorites.
I would call this a try and error thing, I had done something similar but it takes some time customizing as your desired.
So here it's what I believe could solve your main problem:
add_action('pre_get_posts', '_my_pre_get_posts', 10, 1);
function _my_pre_get_posts( $wp ) {
global $typenow, $blog_id;
if ( 'product' == $typenow && $blog_id != 1) {
switch_to_blog(1);
}
}
if the $blog_id is not the main site (reads ID = 1) you should switch to blog ID 1, all this is happening before all post are pulled from the database.
product is the current post_type being queried, if you want this to work with orders as well you probably will check for 'shop_order'.
To know which post_type is being queried you should check the url, Ex: /edit.php?post_type=shop_order.
This probably would bring some issues such as the title of the blog in the adminbar is the one from the main site, and it's missing that at some point it needs to restore to the main site back, but at least this should do the trick.
check this Solution and Let me know how end up
Related
I need to alter the search behavior of a WordPress site where the theme doesn't use searchform.php at all (it's the Divi theme). I'd rather not employ a plugin to do this as the site has many plugins already.
The default state of the page is to show "all posts from all categories" (/?cat=0) with a category list dropdown generated using wp_dropdown_categories() that allows the user to select other categories.
The desired use case is:
The user selects a category from a dropdown
The browser goes to the selected category's archive
The user types a search query into the search box on the category archive with the intent of seeing only posts and pages in that category that contain the search term.
I figured I could use the pre_get_posts hook to do this. So I did the following:
function search_by_cat()
{
global $wp_query;
if ( is_search() ) {
$currenturl = add_query_arg( $wp->query_vars, home_url( $wp->request ) );
$previousurl = wp_get_referer();
if ( strpos( $previousurl, '/category/' ) !==false ) {
$searchcategoryurl = untrailingslashit( $previousurl ) . $currenturl;
wp_redirect( $searchcategoryurl );
exit();
}
}
}
add_action('pre_get_posts', 'search_by_cat');
But this just results in ERR_TOO_MANY_REDIRECTS in the browser. I think it might be because the user is submitting a search that results in the same destination as the referrer. How do I break out of the redirect loop so I can (essentially) redisplay the page of results? Or is my entire approach all wrong? Is there a better way?
Here's the general idea from my comments, you'll need to change the CPT and taxonomy types to match your code. Tax queries are a little weird because of the nested array syntax. The comments should hopefully help understand it more.
add_action(
'pre_get_posts',
static function (WP_Query $query) {
// We only want to run on the main search query
if (!$query->is_search() || !$query->is_main_query()) {
return;
}
// Attempt to get the parameter, or default to 0
$category_id = (int)($_GET['category_id'] ?? 0);
if (!$category_id) {
return;
}
// Since we're search by a taxonomy, we probably want to bind it to a type, too
$query
->set(
'post_type',
// Change as needed
'events'
);
$query
->set(
'tax_query',
[
// See this for specifics: https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters
[
// Change as needed
'taxonomy' => 'event-type',
'field' => 'term_id',
'terms' => [$category_id],
],
]
);
}
);
I'm trying to make my first plugin and I got stuck. Here is the idea.
When my plugin is activated it will create one post type and two taxonomies for that post type (in my case the post type name is 'Ads').
Also, I created two template pages, one to display the listing of all ads post type articles and the other for a single page for the same post type.
Now my problem is how to tell WordPress to look for the templates from a plugin folder rather than the theme folder when the plugin is active.?
Is it something I can do in the plugin file or I have to create another file for this purpose?
This should do what you are looking for:
First, this hook to tell WordPress which is your single CPT template in your plugin
From this answer you get the single_template hook and how to load it.
Define a constant to replace "plugin_dir_path( FILE )" if you use it elsewhere in your plugin, like this:
define('YOUR_PLUGIN_DIR_PATH', trailingslashit(plugin_dir_path( __FILE__ )) );
https://wordpress.stackexchange.com/questions/17385/custom-post-type-templates-from-plugin-folder
function load_single_ad_template( $template ) {
global $post;
if ( 'ads' === $post->post_type && locate_template( ['single-ads.php'] ) !== $template ) {
/*
* This is an 'ads' post
* AND a 'single ad template' is not found on
* theme or child theme directories, so load it
* from our plugin directory from inside a /templates folder.
*/
return YOUR_PLUGIN_DIR_PATH . 'templates/single-ads.php';
}
return $template;
}
add_filter( 'single_template', 'load_single_ad_template', 10, 1 );
And then for the ads archive template, the 'archive_template' hook, like this:
function load_archive_ads_template( $archive_template ) {
global $post;
if ( is_post_type_archive ( 'ads' ) ) {
$archive_template = YOUR_PLUGIN_DIR_PATH . 'templates/archive-ads.php';
}
return $archive_template;
}
add_filter( 'archive_template', 'load_archive_ads_template', 10, 1 ) ;
Official documentation:
https://developer.wordpress.org/reference/hooks/type_template/
https://codex.wordpress.org/Plugin_API/Filter_Reference/archive_template
This is untested, but should work, however, let me know.
I am creating my own plugin in Wordpress. With register_post_type I can view my own posts. I also created a post status with register_post_status. When I go to the summary page I can see all posts and filter on the status.
The "problem": When I go to the summary page, I can see all posts and the filters. The "All" status is always selected on default, but I want to select my custom status on default. Is this possible? Or change the URL in the menu to post_status=&post_type= ? I am talking about the admin side.
Hope someone can help me, because I can't figure it out.
I have fixed it with this code - it changes the url in the menu:
add_action( 'admin_menu', 'wpse_admin_menu', 100 );
function wpse_admin_menu()
{
global $menu, $submenu;
$parent = 'parent';
if( !isset($submenu[$parent]) )
return;
foreach( $submenu[$parent] as $k => $d ){
if( $d[0] == 'name' )
{
$submenu[$parent][$k][2] = 'edit.php?post_status=status&post_type=type';
break;
}
}
}
I've been pulling my hair out with this all day, please forgive the short description, I just need to validate my sanity!!
As the title says, I'm trying to create two or three different single-product layouts within woocommerce. The minimum is trying to achieve would be to have multiple single-product folders each with their own name and configurations.
No matter which way I try to override the single-product.php and make this file use logic to check for the product_cat and give out templates accordingly, I either the page not loading or what I write is skipped over and the default is loaded.
So far I've been through the following methods multiple times, trying to piece together what may be outdated code or otherwise causing all the fuss:
WooCommerce - How to create multiple single product template based on category?
Woocommerce single product - template by categories
Creating a different template file for certain Product Categories - Wordpress/Woocommerce?
I was more hoping someone may know something about this that I'm obviously missing as there are many articles out there on what to try and most claim success but I'm unable to do so.
[Update] using template_include code from #helgatheviking
No success just yet but here's where I'm up to;
File structure
team-shops is the category I'm trying to get
/mytheme/woocommerce/single-product.php - no changes
/mytheme/woocommerce/content-single-product.php
/mytheme/woocommerce/single-product-team-shops.php - changed line 37 to<?php wc_get_template_part( 'content', 'single-product-team-shops' ); ?>
/mytheme/woocommerce/content-single-product-team-shops.php - added additional id to #product-id (line 39)
/mytheme/woocommerce/single-product-team-shops/ folder with all single product files to change.
As I said above this isn't working but hopefully with what I've provided the problem may be more obvious.
Thanks again for any help :)
[Think I've got it]
Ok so I think I've something that works, at least for now it seems to, still have some further testing to do but any thoughts more than welcome, this is what I've got so far along with a single-product-team-shops folder in my theme
add_filter( 'woocommerce_locate_template', 'so_25789472_locate_template', 10, 3 );
function so_25789472_locate_template( $template, $template_name, $template_path ){
$term_id = 2854;
$taxonomy_name = 'product_cat';
$term_children = get_term_children( $term_id, $taxonomy_name );
foreach ( $term_children as $child ) {
// on single posts with mock category and only for single-product/something.php templates
if( is_product() && has_term( $child, 'product_cat' ) && strpos( $template_name, 'single-product/') !== false ){
// replace single-product with single-product-mock in template name
$mock_template_name = str_replace("single-product/", "single-product-team-shops/", $template_name );
// look for templates in the single-product-mock/ folder
$mock_template = locate_template(
array(
trailingslashit( $template_path ) . $mock_template_name,
$mock_template_name
)
);
// if found, replace template with that in the single-product-mock/ folder
if ( $mock_template ) {
$template = $mock_template;
}
}}
return $template;
}
Use a single-product-custom.php template for any product in the "custom" category:
add_filter( 'template_include', 'so_43621049_template_include' );
function so_43621049_template_include( $template ) {
if ( is_singular('product') && (has_term( 'custom', 'product_cat')) ) {
$template = get_stylesheet_directory() . '/woocommerce/single-product-custom.php';
}
return $template;
}
NB: If you use the same action hooks in your single-product-custom.php template you will get the same look as the default single-product.php. You could 'rename' all the hooks and then could add existing functions (such as those for add to cart buttons, etc) to the new hooks in order to achieve a totally custom look.
I am trying to redirect users to a thank you page after leaving a product review in woocommerce. I know the code
add_filter('comment_post_redirect', 'redirect_after_comment');
function redirect_after_comment($location)
{
return $_SERVER["HTTP_REFERER"];
}
will redirect all comments to a location, but I can't seem to find a filter or hook to specify only woocommerce product reviews. Has anyone done this before? Ideally I would just like to redirect to a standard wordpress page so I can add options and features to that pages template file in the future.
The comment_post_redirect filter has a second parameter. This parameter is the $comment object from which we can grab the post's ID. With the post's ID you can test for post_type and adjust the returned variable accordingly.
add_filter( 'comment_post_redirect', 'redirect_after_comment', 10, 2 );
function redirect_after_comment( $location, $comment ){
$post_id = $comment->comment_post_ID;
// product-only comment redirects
if( 'product' == get_post_type( $post_id ) ){
$location = 'http://www.woothemes.com';
}
return $location;
}