Skip to content

Instantly share code, notes, and snippets.

@bporcelli
Created October 11, 2023 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bporcelli/a0fd9df5486d885ec3875a05c5df3b9e to your computer and use it in GitHub Desktop.
Save bporcelli/a0fd9df5486d885ec3875a05c5df3b9e to your computer and use it in GitHub Desktop.
Render an Elementor button that links to the current user's Dokan store
<?php
/**
* To create an Elementor button that links to the current user's Dokan store:
* 1) Copy/paste the code below into your child theme's `functions.php` file
* 2) Set the URL of the Elementor button to `#dokan-my-store`
*/
/**
* Filters button widgets and replaces #dokan-my-store with the
* current user's Dokan store URL.
*
* @param string $content The widget HTML output.
* @param \Elementor\Widget_Base $widget The widget instance.
* @return string The changed widget content.
*/
function change_button_widget_content( $content, $widget ) {
if ( ! function_exists( 'dokan_get_store_url' ) ) {
return $content;
}
if ( 'button' === $widget->get_name() ) {
$store_url = dokan_get_store_url( dokan_get_current_user_id() );
$content = str_replace( '#dokan-my-store', $store_url, $content );
}
return $content;
}
add_filter( 'elementor/widget/render_content', 'change_button_widget_content', 10, 2 );
/**
* Toggle visibility of the store link button based on whether
* current user is a Dokan vendor.
*
* @param bool $should_render Should the button be rendered?
* @param \Elementor\Element_Base $widget Elementor widget.
* @return bool Should the button be rendered?
*/
function should_render_dokan_store_button( $should_render, $widget ) {
if ( ! function_exists( 'dokan_is_user_seller' ) ) {
return $should_render;
}
if ( 'button' !== $widget->get_name() ) {
return $should_render;
}
$link = $widget->get_settings( 'link' );
$url = $link['url'] ?? '';
if ( '#dokan-my-store' !== $url ) {
return $should_render;
}
return dokan_is_user_seller( dokan_get_current_user_id() );
}
add_filter( 'elementor/frontend/widget/should_render', 'should_render_dokan_store_button', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment