WordPress.org

Ready to get started?Download WordPress

Forums

Include and use PHP class into a wordpress page (8 posts)

  1. difimon
    Member
    Posted 6 months ago #

    Dear all,

    I have a class in PHP named sample.class.php which I want to include in a wordpress page to exploit its functions. I am using the template twentytwelve and I have installed the plugin exec PHP to write php code into wp pages. I tried to do this until now with bad results:

    - I've created a folder called "includes" into this path wordpress/wp-content/twentytwelve and then:

    - I've tried to do this in a wordpress page (but doesn't work)

    <?php require_once('includes/sample.class.php');

    and its error is the following:

    Parse error: syntax error, unexpected ‘<’ in /Applications/XAMPP/xamppfiles/htdocs/wordpress/wp-content/plugins/exec-php/includes/runtime.php(42) : eval()’d code on line 11

    ...and also this:

    include(get_bloginfo('twentytwelve').'/wp-content/twentytwelve/sample.class.php');

    Anything works here. Can you explain me what I have to do in order to solve this mistake?

    Thank you in advance.
    Marco

  2. esmi
    Forum Moderator
    Posted 6 months ago #

    Do not edit the theme itself. First create a child theme for your changes.

  3. difimon
    Member
    Posted 6 months ago #

    I don't want to change the theme. I have a PHP class which allows me to print some text from another website (extract info from linkedin). I don't want to repeat all the code in the page, I just want to inlclude the class and use the functions.

  4. bcworkz
    Member
    Posted 6 months ago #

    We consider even minor file edits, or even messing with theme folder structure as changing the theme. It does not matter whether your changes are visible or not, it's that all files are not exactly as they were published anymore.

    You need to make a child theme, it's quite easy. Then you don't have to worry about re-entering your changes every time WP updates.

    As far as the syntax error, it seems there is some lack of coordination between the included file's content and where you are including it. An included file is exactly equivalent to if you had pasted the file's content right where you put the include line (except for the file's initial <?php)

    Once you create a child theme, including the file in your child's functions.php should get you very close to what you need.

  5. difimon
    Member
    Posted 6 months ago #

    OK thank you so much for the reply. I will make a child theme. After that, you suggest to copy paste my php class into the functions php? Could I also take it in another file or in another folder?

  6. bcworkz
    Member
    Posted 6 months ago #

    No, sorry. I was trying to explain further and just made it more confusing. Yes, separate file please! Only the include statement goes in functions.php.

  7. difimon
    Member
    Posted 6 months ago #

    Ok. Thank you so much. Let's asssume that I am working for my "website prototype" on localhost and after I am able to integrate that php class into wp, I will work on another website which is already online. So, isn't a waste of time do a child theme for that site (on localhost) that I will throw away when I finish my tests? If it is, could you kindly explain just the right include syntax, where to put my php class to let it works and (if case) what plugin I need to install.

    Hope you can help me :D
    Thank another time,

    Marco

  8. esmi
    Forum Moderator
    Posted 6 months ago #

    You would place your php class in your child theme's functions.php file and use the child theme on the live site. If the other site is using a different theme, you need to create child for that theme too, place your php class in the new child theme's functions.php file, upload that child theme to the other site and activate it.

Reply

You must log in to post.

About this Topic