Last active
November 20, 2020 10:38
-
-
Save bentasm1/43afb01c3ee95a84e181 to your computer and use it in GitHub Desktop.
WC Vendors Pro - Advanced Custom Fields Support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* - Courtesy of Ben Lumley - https://www.wcvendors.com/help/topic/howto-add-acf-fields-to-frontend-shop-edit/ | |
* | |
* This will display + save any custom fields applicable to the vendor_shop in question. Second one displays the fields, you can adjust the action it’s attached to in order * to move the fields (see templates/dashboard/store-settings.php in the pro plugin for possible actions). You could also split and display different fields in different * places – see the docs for acf_form, you can select single/groups of fields. | |
* http://www.advancedcustomfields.com/resources/acf_form/ | |
* Also possible via acf_form to alter the markup a bit to make it fit your theme. | |
* | |
* This code is not supported by WC Vendors, use at your own risk and at your own discretion. | |
*/ | |
add_action('wcvendors_settings_after_shop_name', 'wcv_save_acf_fields'); | |
function wcv_save_acf_fields() { | |
acf_form_head(); | |
} | |
add_action('wcvendors_settings_after_seller_info', 'wcv_add_acf_fields'); | |
function wcv_add_acf_fields() { | |
acf_form( array( | |
'form' => false, | |
'return' => false, | |
'post_id' => WCVendors_Pro_Vendor_Controller::get_vendor_store_id( get_current_user_id() ) | |
) ); | |
} | |
// if you have media upload fields (file/image), they won't work, and further, the existing branding uploaders for icon etc break. | |
// this is because acf alters the uploader init to pass a post_id, and vendor users don't have 'edit_post' capabilities, assume you guys are handling this somewhere else | |
// this snippet adds a filter in so that if we are trying to 'edit_post' on our own published vendor store, it will be allowed - restoring media upload. | |
add_filter('user_has_cap', 'wcv_vendor_has_cap', 4, 99); | |
function wcv_vendor_has_cap($allcaps, $caps, $args, $user) { | |
if ($args[0] == 'edit_post') { | |
$post = get_post($args[2]); | |
if ($post->post_type == 'vendor_store' && $post->post_author == $user->ID) { | |
$allcaps['edit_published_posts'] = true; | |
} | |
} | |
return $allcaps; | |
} |
If it works, good enough for us. The right way to do things is the way it works. :-)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// if you have media upload fields (file/image), they won't work, and further, the existing branding uploaders for icon etc break.
// this is because acf alters the uploader init to pass a post_id, and vendor users don't have 'edit_post' capabilities, assume you guys are handling this somewhere else
// this snippet adds a filter in so that if we are trying to 'edit_post' on our own published vendor store, it will be allowed - restoring media upload.
// Might be worth running this past your devs - perhaps there's a better/more 'WCV way' to achieve this?
add_filter('user_has_cap', 'wcv_vendor_has_cap', 4, 99);
function wcv_vendor_has_cap($allcaps, $caps, $args, $user) {
if ($args[0] == 'edit_post') {
$post = get_post($args[2]);
if ($post->post_type == 'vendor_store' && $post->post_author == $user->ID) {
$allcaps['edit_published_posts'] = true;
}
}
return $allcaps;
}