• Resolved unsavouryindividual

    (@unsavouryindividual)


    Hi,
    I’m trying to make a site-specific plugin that displays content via shortcode when the browser resolution is that of a mobile device. I use this in the theme files of another installation, it’s called Mobile Detect on github. The end result is to add a shortcode that displays a MailPoet mailing list form in the middle of content.

    The trouble is that I require once the file within the plugin and end up with an uncaught error saying $detect is undefined. So the file isn’t being included. I tried require once in the custom template of the page itself and I still have no luck.

    What am I getting wrong?

    Thanks

    • This topic was modified 3 years, 2 months ago by Jan Dembowski. Reason: Link moved to link field where it belongs

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • You are probably getting the path to the require once wrong.

    Would help if you shared the code
    1. where you do the require
    2. where you create the object $detect

    p.s. why are you detecting the mobile device when you only care about resolution just use media query css to show a div under a certain resolution
    e.g.

    .my-shortcode.div {
      display:none;
    }
    @media screen and (max-width: 700px) {
      .my-shortcode.div {
        display: block;
      }
    }
    • This reply was modified 3 years, 2 months ago by Alan Fuller.
    Thread Starter unsavouryindividual

    (@unsavouryindividual)

    That’s a fair point @alanfuller. I suppose I want more granular control that won’t be possible with CSS.

    I know that my require is pointing toward the file because I got it wrong the first time and this following code doesn’t give me a ‘not found’ warning. So I assume the file has been included.

        require_once __DIR__ . '/Mobile_Detect.php';
    function subscribe_form(){
    
        if ( $detect->isMobile() ) {
        $form_widget = new \MailPoet\Form\Widget();
    return $form_widget->widget(array('form' => 1, 'form_type' => 'php'));
    }}
    add_shortcode('subscribe', 'subscribe_form');

    $detect is defined in the github file I linked above and I’m able to call it everywhere except within a function in a plugin.

    Why did my embedded link to the github project become “The page I need help with”? Good to be aware of for the future.

    Thank you for your time

    Why did my embedded link to the github project become “The page I need help with”? Good to be aware of for the future.

    I can answer that – links in the body of a request are not allowed, as this is a common spammer tactic – so the moderators move it the link area that is only seen by logged in users.

    $detect

    Is not defined. You need to create the object ( instantiate the class )

    $detect = new Mobile_Detect;

    first

    perhaps it had been defined elsewhere but would be out of scope of the shortcode function

    See the examples https://github.com/serbanghita/Mobile-Detect/wiki/Code-examples

    e.g.

    require_once __DIR__ . '/Mobile_Detect.php';
    
    function subscribe_form(){
        
        $detect = new Mobile_Detect;
    
        if ( $detect->isMobile() ) {
        $form_widget = new \MailPoet\Form\Widget();
    return $form_widget->widget(array('form' => 1, 'form_type' => 'php'));
    }}
    add_shortcode('subscribe', 'subscribe_form');
    Thread Starter unsavouryindividual

    (@unsavouryindividual)

    Thank you @alanfuller. You’re right that the file was present, just that $detect wasn’t defined. Your addition fixed my code!

    Happy holidays

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Trouble with require once in a plugin’ is closed to new replies.