Detect if a plugin was included in a certain page

I´m developing a theme that include plugins. One of this plugins is a contact form. And this plugin have an asociate code to validate input data. Because I don´t know in what pages contact form was been added… I want to find a right way to detect if a plugin is included in a current page.

Sample: page.php

<?php get_header(); ?>

<?php if( is contact form included ) : ?>
    //include php form valdiation...
<?php endif; ?>

//page content code ...

<?php get_footer(); ?>

Note: I don´t want to check if a plugin is activated. The plugin is included with theme, therefore is active. I want to detect if is current using for example in page.php. The issue should be include php form validation only when page include contact form.

Any suggestion or advice are welcome 🙂

Thanks.

2 Answers
2

If I understand correctly you want to include a php file that validates the form input, but only when you need to actually validate something?

In this case, you don’t need to check if the contact form exists on some page – you just need to check if the form data has been posted. All you need to do is add a hidden field to the form, with a unique identifier:

 <input type="hidden" name="my_plugin_contact_form" value="true"/>

This is just to enable us to know when data from your plug-in form has been posted:

<?php if(!empty($_REQUEST['my_plugin_contact_form'])) : ?>
    //perform validation
<?php endif; ?>

I’ve noticed that you want to include the validation check inside a page template. I would recommend instead that you wrap your validation inside a function and hook it onto something like init.

A final note: this is not a replacement for using nonces. The above check is only so we know that validation is required – you should still check:

  1. Nonces: did the data come from our form?
  2. Permissions: does the user have permission to send this data

No.2 is probably less relevant here.

Leave a Comment