0

So I bought a theme at themeforest.com that has some custom templates to use with the Woocommerce plugin. I made a child theme so I don't have to edit anything in this original theme.

What I was able to do so far is overwrite the form-login.php template in my child theme.

What I'm trying to do now is add a custom shortcode, similar to the [woocommerce_my_account] one that will call the form-login.php template that I've overwritten.

The code that creates the shortcodes in the Woocommerce plugin looks like this:

File: /wp-content/plugins/woocommerce/classes/class-wc-shortcodes.php

class WC_Shortcodes {

    public function __construct() {
        add_shortcode( 'woocommerce_my_account', array( $this, 'my_account' ) );
    }

    public function my_account( $atts ) {
        global $woocommerce;
        return $woocommerce->shortcode_wrapper( array( 'WC_Shortcode_My_Account', 'output' ), $atts );
    }
}

File: /wp-content/plugins/woocommerce/classes/shortcodes/class-wc-shortcode-my-account.php

class WC_Shortcode_My_Account {

    public static function get( $atts ) {
        global $woocommerce;
        return $woocommerce->shortcode_wrapper( array( __CLASS__, 'output' ), $atts );
    }

    public static function output( $atts ) {
        global $woocommerce;

        if ( ! is_user_logged_in() ) {

            woocommerce_get_template( 'myaccount/form-login.php' );

        } else {

            extract( shortcode_atts( array(
                    'order_count' => 5
            ), $atts ) );

            woocommerce_get_template( 'myaccount/my-account.php', array(
                'current_user'  => get_user_by( 'id', get_current_user_id() ),
                'order_count'   => 'all' == $order_count ? -1 : $order_count
            ) );

        }
    }
}

How could I add a custom shortcode like that one?

Do I put it in my child theme?

Can I just copy/paste the code from the output function, put it in my functions.php and do add_shortcode('custom_shortcode', 'function_in_my_functions_php'); ? But what about the global variable $woocommerce? Can I access that variable from within my child theme?

Thank you so much if anyone wants to shed some light on this, I'm just starting to learn Wordpress.

1 Answer 1

1

The global $woocommerce can be reached, you just have to write global $woocommerce; before using it. Read this article.

You can define your own shortcode, see the documentation. You can copy/paste the code because it's wrapped in a class so it won't trigger fatal error due to double declaration.

But shortcode do not work that way :

function shortcode_handler($atts) {
  //code goes here
 }
add_shortcode('name_of_shortcode','shortcode_handler');
1
  • Thanks for your help, works perfectly, didn't know it was that simple.
    – Smos
    Jul 28, 2013 at 15:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.