문제

I am developing a shortcode for wordpress. My shortcode and widget both need to connect to an external database in order to pull in formation.

My first question is what hook do i use to connect to an external db. The way I have it set up currently it tells me the header has already been sent. I need it to send with the header to make the clean connect. I do not want to use output buffer if i don't have to. I want to make a clean connect. What hook do i use to make a connect and not get the header error?

My second question is: Is there a way for this hook to only be called on certain pages? I don't want to make this connect on every page just on the pages that contain the short code. Is there an if statement or some sort of hook filter so that the db connect is only made when needed instead of being called on every page load. Thanks.

도움이 되었습니까?

해결책

The "wp" hook is run after the current $post is set but before the headers are output.

So you should be able to do a check like if(strpos($post->post_content, "[shortcode]") !== false) to determine if the DB connect code should run.

You would use that hook like:

add_action("wp", "my_wp"); //where "my_wp" is the name of your function
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top