Eric Brown
Eric Brown

Reputation: 1462

Wordpress Hook into page body

I am working on a plugin that will be used to add a customized form of Acuity Scheduling for a specific page. I want to add the scheduling form after the menu and page title on one particular page. Here is my current code:

add_action( 'template_redirect', 'check_if_acuity_page');

function check_if_acuity_page(){
    if(is_page('Schedule Page')){
    add_action( 'add to acuity', 'display_acuity_scheduling_api');
    }

}

function display_acuity_scheduling_api(){
    echo '<div style="margin-top: 25px;">"Code to add Acuity Schedule to page"</div>';

}

The 'add to acuity' is a custom action hook that is currently added in the header.php file of the theme I am using. It adds the schedule at the very top of the page currently, so I can at least get it on the proper page, but it is located above the Menu and Title for the page. I am working on creating a custom layout and using PHP code to modify the page depending on what the user chooses, which is why I am not just using a simple embed code.

I am new to Wordpress Plugins and Hooks so I am not sure if I am supposed to be using an action or filter hook for this. Any help would be very appreciated.

Upvotes: 0

Views: 4990

Answers (2)

Gert-Jan Kooijmans
Gert-Jan Kooijmans

Reputation: 472

WordPress action hooks are a means of providing a way for other developers to insert their own code in specific locations within your code, in order to change or expand the functionality of your code.
So in this case you should be using an action hook.

The concept of filters and hooks is explained in this article.

So by placing the add_action function in your template after the menu and page title you can hook onto it with a function.

In your page template after the menu and page title:

add_action( 'add to acuity', 'check_if_acuity_page');

In your functions.php:

function check_if_acuity_page() {
  if(is_page('Schedule Page')) {
    echo '<div style="margin-top: 25px;">"Code to add Acuity Schedule to page"</div>';
  }
}

Upvotes: 0

Johny Santiago
Johny Santiago

Reputation: 99

To add code just before content which is below page title use following code:

function check_if_acuity_page(){
    if(is_page('Schedule Page')){
    echo '<div style="margin-top: 25px;">"Code to add Acuity Schedule to page"</div>';}
}
function add_code_before_content($content){
    $acuity_page = check_if_acuity_page();
    $content = $acuity_page.$content;
    return $content;
}
add_filter('the_content','add_code_before_content');

Hope this helps.

Upvotes: 1

Related Questions