SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Not sure how to reference a WP function within my class

    I'm working on a WordPress plugin and I'm a little confused as to how to reference a WP function from within my class. I'm also really new to OOP (but trying to learn) and I'm struggling with static Vs. instantiated classes (if that's even the way to phrase that...).

    Here's basically what I have:

    Main Plugin File:
    PHP Code:
    // Instantiate Platform Class
    require_once("platform.php");
    $platform = new Platform;

    // Show page URL
    echo $platform->store_url(); 
    Platform Class File:
    PHP Code:
    class Platform {

        function 
    store_url() 
        {
            
    // Returns permalink for Page with ID = 5
            // get_permalink() is a WP function
            
    return get_permalink(5);    
        }

    The error I get is:
    Fatal error: Call to a member function get_page_permastruct() on a non-object in ../wp-includes/link-template.php on line 223

    I can see what's going on: get_page_permastruct() is a method of the class WP_Rewrite and there does not appear to be an instance of WP_Rewrite... or at least I don't have access to the instance from within my class.

    I've tried to use global $wp_rewrite within my method but that didn't work.

    What am I doing wrong and how can I fix it?

    Thanks

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)
    I'm pretty much an OOP newbie so I could be way off base, but I think if you want a class to be able to access another's methods you can "extend" it

    PHP Code:
    class Platform extends WP_Rewrite {

        function 
    store_url() 
        {
            
    // Returns permalink for Page with ID = 5
            // get_permalink() is a WP function
            
    return get_permalink(5);    
        }

    or maybe something like

    PHP Code:
    class Platform {

        var 
    $wp_rewrite_obj = new WP_Rewrite;
        var 
    $pagestruct;

        
    $this->pagestruct $this->wp_rewrite_obj->get_page_permastruct();

        function 
    store_url() 
        {
            
    // Returns permalink for Page with ID = 5
            // get_permalink() is a WP function
            
    return get_permalink(5);    
        }



Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •