Willem
Willem

Reputation: 447

Only Add Filter / Function on Certain Pages

I have want to add the following code to my functions.php in WordPress:

add_filter( 'gform_submit_button_4', 'form_submit_button', 10, 2 );
function form_submit_button( $button, $form ) {
    return "<button class='button' id='gform_submit_button_{$form['id']}'>Get a Test Drive</button>";
}

But, I only want to apply it on certain pages, I've tried two avenues: a if statement using is_page & wrapping the gravity forms shortcode in a div with a class and targeting only .class #gform_submit_button_4 so far I haven't been able to get either to work.

Any help would be greatly appreciated.

Thanks
Willem

Upvotes: 0

Views: 711

Answers (2)

Shahid Latif
Shahid Latif

Reputation: 135

In gravity form add a field of page slug with type hidden from back-end option of gravity form.

1) You can create field from back-end and assign it a value with JS

2) Gravity form give you an option to send url with form you can parse page slug from that url

add_filter( 'gform_submit_button_4', 'form_submit_button', 10, 2 ); function form_submit_button( $button, $form ) { // please print $form and check you get your current page slug variable e.g $form['my_page_slug'] // create an array that have your new button slugs e.g $pages_slug_array = array('about','contact-us'); if(in_array($page_slug_arry, $form['my_page_slug'])){ return $button = "<button class='button' id='gform_submit_button_{$form['id']}'>Get a Test Drive</button>"; } }

Hope you got that

Upvotes: 1

Milap
Milap

Reputation: 7211

Try to use get_queried_object function,

<?php
function form_submit_button( $button, $form ) {
    return "<button class='button' id='gform_submit_button_{$form['id']}'>Get a Test Drive</button>";
}
$queried_object = get_queried_object();
if ( $queried_object ) {
    $pageId = $queried_object->ID;
    if($pageId == 'someid') {
        add_filter( 'gform_submit_button_4', 'form_submit_button', 10, 2 );
    }
}
?>

Upvotes: 1

Related Questions