I am studying how to make a plugin for wordpress, I could make a plugin that adds shortcodes to place on my page and it generates the form.
function mf_container($atts, $content = null) {
$content = do_shortcode($content);
$content = str_replace("<br />","",$content);
$content = str_replace("<p>","",$content);
$content = str_replace("</p>","",$content);
return
"<form action='".plugin_dir_url( __FILE__ )."mult-form-submit.php' method='POST'>" . $content . "</form>";
}
add_shortcode('mult_form','mf_container');
Now precisso make this form is saved in the bank related to the logged in User.
The way that the code above when I click submit it redirects me to a page to "multi-form-submit.php". However when I get on that page I have the following code in it:
<?php if ( ! defined( 'ABSPATH' ) ) die("Not permited");
global $wpdb, $current_user;
$current_user = wp_get_current_user();
however she has not returned since this more in the context of worpress, anyone can do it?
I managed to find a reference that has a light and after many attempts found how to do. That was the reference: link
I am using a plugin structure generated by WORDPRESS PLUGIN BOILERPLATE GENERATOR
In it you should put what started in public view within the function: define_public_hooks(). In this role I had a stretch that was checked that enabled the option to use my plugin:
/** Set build form by shorcodes*/
if($this->get_option('mult_form')){
$this->loader->add_action('init', $plugin_public, 'mult_form');
}
this section redirects me to a function of a class that controls all my public actions:
public function mult_form(){
if(isset ( $_REQUEST['action']) and $_REQUEST['action'] == 'mult_form_submited'){
$this->mult_form_submit();
}
$this->mult_form_shortcode();
}
call it two private functions, where do some treatments that will not show the code, and if all goes well, make a include_once('partials / my_file.php');, ed a flame a different file of course, being first where do all the treatments to generate the shortcode and the second where I will put all my treatments submission form.
Related
I am developing a plugin where user will submit a form. The form submits to the same page. Form data handling code is implemented by checking the condition-
if ( !empty( $_POST['action'] ) && $_POST['action'] == 'customer_add_new')
Then the data is validated and entered to database using $wpdb->insert.
After that, I want to pass a variable to the URL, so that success message can be displayed and user can be stopped from re-submitting the form by refreshing the page. For that, I used-
$url = add_query_arg( array(
'customer_add_new' => 'success'
) );
wp_redirect( $url );
exit();
But this code is throwing the following error-
Warning: Cannot modify header information - headers already sent by (output started at E:\wamp\www\cp_plugin\wp-includes\class.wp-scripts.php:343) in E:\wamp\www\cp_plugin\wp-includes\pluggable.php on line 1216
I have successfully used the same code in the theme for this very same project. But the functionality was better suited for a plugin, than for a theme.
hook admin_post will help you. Works the same as admin-ajax.php.
Change form action to <?php echo admin_url('admin-post.php'); ?> and create hooks:
add_action( 'admin_post_{action}', 'funct' );
add_action( 'admin_post_nopriv_{action}', 'funct' );
function funct() {
if ( wp_verify_nonce( 'some_nonce', 'some_action' ) ) {
// Your code using $_POST
// And create wp_redirect();
}
}
This might help you.
I have been building a plugin for WordPress for a client, however I seem to be having a strange problem. I have added an options page but it's not working correctly.
When I go to the Wordpress menu I can see my options page. It has the correct options page options-general.php?page=Beacon_Registation_WP, however when I click on the menu item that page redirects me to upgrade.php?_wp_http_referer=%2Fwp-admin%2Foptions-general.php%3Fpage%3DBeacon_Registation_WP
Am unsure why this is happening.
The code im using as the entry point for the plugin:
<?php
/**
* Plugin Name: ** OMITED **
* Plugin URI: ** OMITED **
* Description: This plugin enabled the wordpress site to register new users to the beaconapp
* Version: 1.0
* Author: Martin Barker - ** OMITED **
* Author URI: ** OMITED **
* License: Propriatry Software (do not distribute)
*/
class BeaconRegistation
{
// handler for registing the wp-admin menu
public function addMenuItem()
{
add_options_page( 'Configure Server', 'BeaconApp Registation', 'manage_options', 'Beacon_Registation_WP', array($this, "wp_admin") );
}
// display the wp_admin page for this plugin
public function wp_admin()
{
ob_start();
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
$this->sdr = get_option("sdr", "martindev.** OMITED **");
if($_POST['sdr'] !== $this->sdr){
update_option("sdr", $_POST['sdr']);
$this->sdr = get_option("sdr", "martindev.** OMITED **");
}
// include the view for switch environment
include("admin.php");
return ob_get_clean();
}
// displays the shortcode 'ba_business_form'
public function showBusinessForm($atts, $content = "")
{
ob_start();
if($_POST['form_mode'] == "business"){
// form has been posted
}else{
include("business.html");
}
return ob_get_clean();
}
// displays the short code 'ba_agency_form'
public function showAgencyForm($atts, $content = "")
{
ob_start();
if($_POST['form_mode'] == "agency"){
// form has been posted
}else{
include("agency.html");
}
return ob_get_clean();
}
public function init()
{
// add the admin menu call
add_action( 'admin_menu', array($this, "addMenuItem"));
// add the short code for the business form
add_shortcode("ba_business_form", array($this, "showBusinessForm"));
// add the short code for the agency form
add_shortcode("ba_agency_form", array($this, "showAgencyForm"));
}
}
(new BeaconRegistation())->init();
Just to confirm the 2 shortcodes are working correctly so the only problem is the options_page
So after some hacking of the Wordpress admin panel, we managed to dig this down to the include("admin.php") I'm unsure how but WordPress seems to interfere and prevent this process from working.
so the include relative paths seem to work as it does with the include("business.html"); and include("agency.html"); however the "admin.php" one just seems to not occur it's like another admin.php get's included unsure how or why this happens.
so to fix i resolved the absolute path and included via that include(realpath(dirname(__FILE__))."/admin.php");
I created a new page in my admin panel.
On this page, for example function is_admin() works fine.
if ( ! is_admin() ) {
echo "You are viewing the theme";
} else {
echo "You are viewing the WordPress Administration Panels";
}
From this page i send post data to xxx.php file.
And! In this xxx.php file functions such as is_admin doens't work.
How can i let wordpress to understand, that this xxx.php file is the part of him? So i can use functions on this page?
I am using
"include wp-load.php"
but it doesn't help me.
You can add him in the theme folder & include in functions.php:
// functions.php of your current theme
include __DIR__.'/custom.php';
I just checked the source code on Github.
function is_admin() appears in this file https://github.com/WordPress/WordPress/blob/6fda2e67b0fe3872cbf5f82b58b98f2a4c96d8f8/wp-includes/load.php#L710-L717
So, require_once 'wp-includes/load.php; should sort you out.
The fact that you're sending POST data (request, i assume) to another php file makes me think that it's a completely different HTTP request, therefore you won't be able to use wp functions in the target php file, unless you include wp-load.ph in that file.
See this: https://wordpress.stackexchange.com/questions/69184/how-to-load-wordpress-on-non-wp-page
add_action('admin_menu', function(){
add_menu_page( 'Subscribe', 'Subscribe', 'manage_options', 'subsc-options', 'sub_setting', '', 4 );
} );
add_action( 'current_screen', function( $current_screen ){ // hook is admin panel only
if ( stripos($current_screen->base, 'subsc-options')==true ) {
include __DIR__.'/custom.php';
}
});
<form method="post">
<input type="hidden" name="my_save_settings" >
..............
</form>
/********* CUSTOM.PHP **************/
if ( isset($_POST['my_save_settings']) ) {
// save form fields
}
I'm getting a little ahead of my php experience. Please help.
I am trying to create a custom shortcode for contact form 7 - using that plugins "developer cook book"
I want the shortcode to get the url (permalink) of the page the form is on
and then use this shortcode in the message body of the return email or that's to say the email notification to the business - and in this way the business will know which landing page this form was submitted from, since this form will be used on many landing pages.
here's the code I have thus far, in my functions.php file:
add_action('wpcf7_init', 'custom_add_shortcode_lptitle');
function custom_add_shortcode_lptitle() {
wpcf7_add_shortcode('lptitle', 'custom_lptitle_shortcode_handler'); // "lptitle" is the type of the form-tag
}
function custom_lptitle_shortcode_handler($tag) {
global $post;
$url = get_permalink($post->ID);
return $url;
}
And then the shortcode I am using in the message body of the notification email is: Landing Page URL [lptitle]
Add below code in your functions.php
wpcf7_add_shortcode('lptitle', 'custom_lptitle_shortcode_handler', true);
function custom_lptitle_shortcode_handler( $tag ) {
global $post;
$url = get_permalink($post->ID);
return $url;
}
Your shortcode is
[lptitle]
I'm building a contact form in SilverStripe.
When testing validation, if I leave the required fields blank and hit submit, those input fields will be added a .holder-required class. Even if I reload the page, they won't disappear. (actually the error messages *** is required will stay there after reload too. I just stopped the messages from showing).
I've searched the whole project folder, but there's no file that even has holder-required in it.
Where does the .holder-required class come from?
The reason why you couldn't find holder-required is because it technically doesn't exist in the SilverStripe codebase, it actually is a class that is concatenated together from two strings.
In a FormField, there is a function called "extraClass" which adds these classes to the field.
Below is the snippet of code from the FormField class:
public function extraClass() {
$classes = array();
$classes[] = $this->Type();
if($this->extraClasses) {
$classes = array_merge(
$classes,
array_values($this->extraClasses)
);
}
if(!$this->Title()) {
$classes[] = 'nolabel';
}
// Allow custom styling of any element in the container based on validation errors,
// e.g. red borders on input tags.
//
// CSS class needs to be different from the one rendered through {#link FieldHolder()}.
if($this->Message()) {
$classes[] .= 'holder-' . $this->MessageType();
}
return implode(' ', $classes);
}
What this tells us is that for a message that appears for a field, it will append holder-{Whatever_Your_Message_Type_Is} as an extra class.
The reason why $this->Message() would still be set after a page reload is that the error information is actually saved to the session for that form.
Below is a snippet from the Form class which is what calls the FormField::setError(), the function that sets the message property on the form field.
public function setupFormErrors() {
$errorInfo = Session::get("FormInfo.{$this->FormName()}");
if(isset($errorInfo['errors']) && is_array($errorInfo['errors'])) {
foreach($errorInfo['errors'] as $error) {
$field = $this->fields->dataFieldByName($error['fieldName']);
if(!$field) {
$errorInfo['message'] = $error['message'];
$errorInfo['type'] = $error['messageType'];
} else {
$field->setError($error['message'], $error['messageType']);
}
}
// load data in from previous submission upon error
if(isset($errorInfo['data'])) $this->loadDataFrom($errorInfo['data']);
}
if(isset($errorInfo['message']) && isset($errorInfo['type'])) {
$this->setMessage($errorInfo['message'], $errorInfo['type']);
}
return $this;
}
I've had a bit more of a browse of the Form code, it should be clearing the errors after it is rendering the form. There are two functions part of the form class, clearMessage and resetValidation.
The function clearMessage is called when rendering the form template through forTemplate. I do not see any usage of the resetValidation function throughout the SilverStripe CMS or Framework codebases.
You may need to potentially call one or the other in your code if under your circumstances the message is not clearing.