WordPress.org

Ready to get started?Download WordPress

Forums

This function breaks the entire page afterwards (4 posts)

  1. Jiri Krewinkel
    Member
    Posted 1 year ago #

    Okay so I'm trying to create a wordpress shortcode called chapter, i.e. [ chapter ]text [ /chapter ] (without the spaces)

    What this does is it first checks if the content entered is already in the database, if it's not it will add it to the database and will also add a count to another table. Then it outputs this in a div

    This is the code I have

    http://pastebin.com/h6ddEK1t

    When I apply this in a post, the entire content of the post doesn't show up on the page and everything afterwards isn't rendered as well (so no footer etc.) However the div that the the_content(); is in is closed of like it should though

    Can anyone offer me any insight into what I have been doing wrong? I'm pretty new to PHP

  2. Andrew Bartel
    Member
    Posted 1 year ago #

    Can you provide a link to a page that you're using it on? Preferably with as little else going on as possible.

  3. toocoolone
    Member
    Posted 1 year ago #

    In my experience, when this happens there was a database error. Considering the code you pasted is loaded with direct calls to MySQL this is likely the problem.

    Read up on the $wpdb object in the Codex -- it acts as an abstraction layer between MySQL databases and WP and prevents you from being vulnerable to injection attacks -- which as you have it you are very vulnerable! Even if the variables are internal you should still prepare your statements properly to prevent errors and the problem at hand.

    Are these tables you are querying in the same database as WP? Are they in another database? Bare calls to mysql_query() will act on whatever the last connection is (usually $wpdb), in which case they may be failing to find the required table. Also, if you've connected correctly to the other database, if you don't mysql_close() out of that connection correctly, when WP tries to pull the footer information (widgets and the like) it will pull from the last database connection and not find what it is expecting and quietly die.

  4. Jiri Krewinkel
    Member
    Posted 1 year ago #

    Hey guys, thanks for the reaction. I posted the problem at the WordPress Stack Exchange here as well: http://wordpress.stackexchange.com/questions/57645/custom-shortcode-breaks-my-page

    the second reaction is what really fixed it.

    Apparently I was trying to access the database in an incorrect way just like you said, toocoolone.

    I was also trying to access the post id variable which was set in a global variable.

    After some hours I finally got it to work though, my first real php script after a week of php learning haha I'm pretty proud!

    Thanks for the help though!

Topic Closed

This topic has been closed to new replies.

About this Topic