Wordpress Hook into page body - wordpress

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.

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.

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>';
}
}

Related

Using timber to render plugin pages

I am making a plugin which adds a bunch of pages to the admin view of WP. I'd really like to use Timber, specifically, the Twig templating functionality to render these pages.
While I have next to zero experience in WP and PHP in general, what attracts me to this approach is my previous familiarity with Django / Flask templates which allow me to extend a base template and specify blocks for header, content, footer. That seems trivial to do with Timber when using it to create a theme, but I can't for the life of me figure out how to make this setup work within a plugin. Sure, I can do something like this:
add_action( 'admin_menu', 'test_setup_menu' );
function test_setup_menu() {
add_menu_page(
'Tables',
'Tables',
'manage_options',
'test-tables',
'admin_page_test'
);
}
function admin_page_test() {
Timber::Render( 'test.twig');
}
But that of course will render test.twig with header and footer parts already populated from the theme. The issue specifically is that I want to be able to add information to the header or footer blocks. I know I can do this like so:
add_action('admin_head', 'add_to_head')
function add_to_head() {
...
}
But this is precisely the type of thing I'm trying to avoid, I wish to encapsulate this type of logic in a Twig template. Is there any way to make this work?
Here's an example of how to add a custom admin page for a plugin.
<?php
/**
* Plugin Name: Test Run
*/
add_action('admin_menu', 'admin_menu_cb');
function admin_menu_cb()
{
// Ref: https://developer.wordpress.org/reference/functions/add_menu_page/
add_menu_page('Test Run Admin Page', 'Test Run', 'manage_options', 'test-run', 'render_menu_page_cb', 'dashicons-schedule', 3);
}
function render_menu_page_cb()
{
Timber::$locations = __DIR__.'/views';
$data = [];
Timber::render('main.twig', $data);
}
For a more full example please see the below repo. I created it recently as a guide for anyone to use Timber in a wordpress plugin.
https://github.com/chanakasan/a-wordpress-plugin-using-timber

Using do_shortcode() with contact form 7 and form tag is missing

I’m trying to use the contact form in my template using do_shortcode and popup (Request availability button on a page). But I’ve noticed that form generated only inputs without <form> tag as usual so it failed to submit.
That’s how I’m implementing it in functions.php
function wc_shop_popup() {
$reqform = '[contact-form-7 id="987" title="Request availability"]';
echo '<a class="button button_full_width button_left req_button popup-link" href="#popup-availability" rel="lightbox" data-type="inline"><span class="button_icon"><i class="icon-layout"></i></span><span class="button_label">Request Availability</span></a>
<div id="popup-availability" class="popup-content mfp-hide"><div class="popup-inner" style="padding:20px;">'.do_shortcode($reqform).'</div></div>';
}
add_action( 'woocommerce_after_add_to_cart_button', 'wc_shop_popup' );
This is the page - Request availability button. Any suggestions?
The problem was that contact form has been generated inside of add to cart form. I used another woocommerce hook called "woocommerce_after_single_product_summary" instead of "woocommerce_after_add_to_cart_button" and it's working!

Add custom button in profile page of BuddyPress

I tried adding a custom button in BuddyPress profile page using below mentioned code. But when I added that code, only Name & Profile picture was appearing & no other content generated through BuddyPress was appearing:
function mapbtn_custom_button() {
echo '<div class="mapbtn">map<div>';
}
add_filter( 'bp_before_member_header_meta', 'mapbtn_custom_button' );
You're trying to hook a filter to an action call.
Try this instead:
add_action( 'bp_before_member_header_meta', 'mapbtn_custom_button' );

Make Wordpress Pages's Titles readonly

I'm looking for a WP function that add the Read-only parameter to all Pages's Titles's input, that will make the Page's title unalterable.
Thanks a lot in advance.
This can be accomplished with some simple JavaScript/jQuery. Create a file called admin_title_disable.js, and queue it up within functions.php. For example:
functions.php:
wp_register_script('admin_title_disable', '/path/to/admin_title_disable.js');
function disableAdminTitle () {
wp_enqueue_script('admin_title_disable');
}
add_action('admin_enqueue_scripts', 'disableAdminTitle');
Now, in your js file:
jQuery(document).ready(function ($) {
$('#title').attr('disabled','disabled');
});
This will set both post and page title input fields with a disabled attribute. Hope this helps!
If you want to restrict this script to a particular admin page, wrap the add_action hook in a conditional that compares $_GET['page']. You can also take advantage of the $hook parameter that is available when using admin_enqueue_scripts to check for the page. See here.
Update::
WordPress makes it a little tricky to tell between post and page edit screens, but there is a hidden input that you can take advantage of. :) Here's an updated version of the jQuery that will only run on page edit screens:
jQuery(document).ready(function ($) {
//find the hidden post type input, and grab the value
if($('#post_type').val() === 'page'){
$('#title').attr('disabled','disabled');
}
});
No need to make a seperate js file. Adding this to your function.php will do the same that Matthew showed.
function admin_footer_hook(){
?>
<script type="text/javascript">
if(jQuery('#post_type').val() === 'post'){
jQuery('#title').prop('disabled', true);
}
</script>
<?php
}
add_action( 'admin_footer-post.php', 'admin_footer_hook' );
This Solution Will disable clicking on the post title and editing it using CSS. CSS targets post type "page" only. It has been tested on Gutenberg visual editor. Users Can still edit title from "Quick Edit".
Add this code to your functions.php file.
function disable_title_edit() {
if(!current_user_can('administrator')){
if( !current_user_can('administrator')){ ////Only allow Admin
echo '<style>.post-type-page .edit-post-visual-editor__post-title-wrapper{
pointer-events: none;
}</style>'; } }
}
add_action('admin_head', 'disable_title_edit', 100);

display custom fields automatically when a custom post type is displayed

I am trying to automatically display all the custom fields of a custom post type alongside it's title and content.(Not in admin but on my actual site)
I need to be able to do this with an action hook or filter, rather than creating a template.
After scouring the web I was able to find the 'publish_{custom_post_type_name}' hook:
function my_cool_hook() {
echo get_post_meta($post->ID, 'my-custom-field-name', true);
}
add_action( 'publish_past_symposia', 'my_cool_hook' );
but it doesn't seem to do anything when I view my published custom post type on my site. Any ideas?
add_action( 'publish_past_symposia', 'my_cool_hook' );
This hook triggered only if PUBLISH post type.
YOu need to trigger the hook on web part - so...
add_filter('the_content', 'my_cool_hook');
function my_cool_hook($content){
return $content.get_post_meta(get_the_id(), 'my-custom-field-name', true);
}
now the content body filtred and your string from custom fields added.

Resources