2

I'm writing my first plugin in attempt to learn more about the backend of EE and Codeigniter. My PHP is enough to get by, but I'm no expert.

This plugin is as follows:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$plugin_info = array(
    'pi_name'       => 'YouTube ID from URL',
    'pi_version'        => '0.1',
    'pi_author'     => 'Dan Grebb',
    'pi_author_url'     => 'http://dgrebb.com/',
    'pi_description'    => 'Returns a YouTube ID from a variety of youtube share link types.'
);

class Youtube_id_from_url {

    public $return_data = "";

    public function __construct($urltag = NULL)
    {
        $this->EE =& get_instance();

        if (empty($urltag))
        {
            $urltag = $this->EE->TMPL->tagdata;
        }

        function sliceurl($url) {
            $pattern = 
                '%^# Match any youtube URL
                (?:https?://)?  # Optional scheme. Either http or https
                (?:www\.)?      # Optional www subdomain
                (?:             # Group host alternatives
                  youtu\.be/    # Either youtu.be,
                | youtube\.com  # or youtube.com
                  (?:           # Group path alternatives
                    /embed/     # Either /embed/
                  | /v/         # or /v/
                  | /watch\?v=  # or /watch\?v=
                  )             # End path alternatives.
                )               # End host alternatives.
                ([\w-]{10,12})  # Allow 10-12 for 11 char youtube id.
                $%x'
                ;
            $result = preg_match($pattern, $url, $matches);
            if (false !== $result) {
                return $matches[1];
            }
            return false;
        }

        $youtubeid = sliceurl($urltag);

        $this->return_data = $youtubeid;
    }
}

/* End of file pi.plugin_name.php */
/* Location: ./system/expressionengine/third_party/plugin_name/pi.plugin_name.php */

Then I have this template code

{exp:stash:set name="video-gallery"}
    {exp:query sql="SELECT cat_id FROM exp_categories WHERE cat_url_title = '{segment_2}'"}
        {exp:channel:entries
            channel="videos"
            disable="category_fields|member_data|pagination|trackbacks"
            dynamic="no"
            category="{cat_id}"
        }
            <div class = "video_El even {switch='|last'}">
                {exp:youtube_id_from_url}{video-url}{/exp:youtube_id_from_url}
                <h3>{title}</h3>
            </div>
        {/exp:channel:entries}
    {/exp:query}
{/exp:stash:set}

The error message is as follows:

Fatal error: Cannot redeclare sliceurl() (previously declared in /local.dev/system/expressionengine/third_party/youtube_id_from_url/pi.youtube_id‌​_from_url.php:24) in /local.dev/system/expressionengine/third_party/youtube_id_from_url/pi.youtube_id‌​_from_url.php on line 24

I really appreciate any help I can get. Thanks to all for such a great community!

  • On what line number is your error? – Justin Kimbrell Dec 2 '12 at 1:33
  • Here's the error: Fatal error: Cannot redeclare sliceurl() (previously declared in /local.dev/system/expressionengine/third_party/youtube_id_from_url/pi.youtube_id_from_url.php:24) in /local.dev/system/expressionengine/third_party/youtube_id_from_url/pi.youtube_id_from_url.php on line 24 – dgrebb Dec 2 '12 at 1:56
  • It's best to edit your question to include the error message; at the moment it's unclear what your question is without reading the comments. – Adrian Macneil Dec 2 '12 at 2:12
8

You shouldn't be declaring functions inside your __construct() method.

Move sliceurl a bit further down, and call it using $this->sliceurl (since it's part of your class):

class Youtube_id_from_url {

    public $return_data = "";

    public function __construct($urltag = NULL)
    {
        $this->EE =& get_instance();

        if (empty($urltag))
        {
            $urltag = $this->EE->TMPL->tagdata;
        }

        $youtubeid = $this->sliceurl($urltag);

        $this->return_data = $youtubeid;
    }

    function sliceurl($url) {
        // etc
    }
}
2

Update:

Ah, heh. Yes. What @AdrianMacneil says above. Didn't see the forest for the trees on that one.


I've tested your plugin locally, and the only error I'm seeing is on line 43, where you're only testing for the preg_replace to return false on error, where you should also be looking for it to return "0" for no matches:

if ( $return !== FALSE && $return !== 0 ) {
    return $matches[1];
}
return FALSE;

However, based upon your comment above, it doesn't look like the actual plugin file is the problem. I'd recommend that when you test add-ons that you're developing, that you make the test as simple as possible (as opposed to testing it while it's wrapped within 3 other tags).

Regarding the error you're seeing (the redeclaration of the sliceurl() function)—are you using another plugin that also declares this function? To get around this for the present, you could wrap the sliceurl() declaration like this:

if ( ! function_exists('sliceurl') ) {
    function sliceurl($url) {
         ...
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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