• Posting this in case it’s helpful to someone else.

    I found myself in a situation where I needed to extract the logout URL from the anchor tag returned from the [llrmlogout] shortcode. So, I created this quick function. Just modify to work with all the other shortcodes provided by this plugin.

    
    // Function to extract the logout url from the login-logout-register-menu plugin shortcode
    function get_logout_url() {
    
        if ( !shortcode_exists( 'llrmlogout' ) ) {
            return;
        }
    
        $dom = new DOMDocument;
    
        $logout_html = do_shortcode( '[llrmlogout]' );
    
        $dom->loadHTML($logout_html);
    
        $node = $dom->getElementsByTagName('a')->item(0);
    
        return $node->getAttribute( 'href' );
    
    }
    add_shortcode('get-logout-url', 'get_logout_url');
    
Viewing 1 replies (of 1 total)
  • Thread Starter jamesryancooper

    (@jamesryancooper)

    Actually, we need to add a check to make sure that the shortcode is returning something or an error will be thrown if we provide loadHTML an empty variable. So, here’s the updated function with the check:

    // Function to extract the logout url from the login-logout-register-menu plugin shortcode
    function get_logout_url() {
    
        if ( !shortcode_exists( 'llrmlogout' ) ) {
            return;
        }
    
        $dom = new DOMDocument;
        $logout_html = do_shortcode( '[llrmlogout]' );
    
        if  ( !empty($logout_html) ) {
            $dom->loadHTML($logout_html);
            $node = $dom->getElementsByTagName('a')->item(0);
            return $node->getAttribute( 'href' );
        } else {
            return;
        }
    
    }
    add_shortcode('get-logout-url', 'get_logout_url');
Viewing 1 replies (of 1 total)
  • The topic ‘Extracting URL from Shortcode’ is closed to new replies.