Contact Form 7 custom shortcode - php

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]

Related

How to Make a PHP Variable Return Text Instead of Nothing

My code needs to see and use the text held in a variable:
I'm working on a WordPress plugin. One section of it needs to place text above the website's login form. If I just type in text as in the code below (return "My New Header Text";) it works fine and "My New Header Text" appears above the form.
// Add a CUSTOM MESSAGE to the top of the WordPress login form
function new_plugin_login_message( $message ) {
if ( empty($message) ){
return "My New Header Text";
} else {
return $message;
}
}
add_filter( 'login_message', 'new_plugin_login_message' );
But when I use a variable that includes the text, as in: (return "$MyNewMessage";) it won't work. Below is what I have. Even with lots of experimentation and Googling, I can't find a way for it to actually take the text stored in the variable and place it above the form. How do I make it see and use the text that is in the variable, "$MyNewMessage"?
// Add a CUSTOM MESSAGE to the top of the WordPress login form
function new_plugin_login_message( $message ) {
if ( empty($message) ){
return "$MyNewMessage";
} else {
return $message;
}
}
add_filter( 'login_message', 'new_plugin_login_message' );
This must be easy, but it eludes me. Thanks for any assistance.

How to hide "You are now logged out" message on Wordpress login form?

Expectation:
I want to hide the login message that shows up on the Wordpress login form after a user has logged out. Added screenshot of the message that I am trying to hide.
https://i.imgur.com/vJm56sI
Wordpress Version 5.4.2
Wordpres theme: Oceanwp 1.8.9
I have used the below mentioned code in the functions.php file of my theme.
add_filter( 'wp_login_errors', 'my_logout_message' );
function my_logout_message( $errors ){
if ( isset( $errors->errors['loggedout'] ) ){
return null;
}
return $errors;
}
Error Recieved:
I have provided the screen shot of the error that I have recieved.
https://i.imgur.com/xlpGiwM.png
Also, the login form isn't visible after user logout.
Yes hii interesting, perhaps it might be useful to override the error ID loggedout:
add_filter('wp_login_errors', 'my_logout_message');
function my_logout_message($errors) {
// Remove the message
$errors->remove('loggedout');
// Or customize the message:
if (array_key_exists('loggedout', $errors->errors)) {
$errors->errors['loggedout'][0] = 'My message';
}
return $errors;
}

Create wordpress plugin that processes and submit form

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.

wordpress contact form 7 disable email

I am facing an issue with Contact Form 7 for Wordpress. I want to disable the email notification which i did using
demo_mode: on
At the same time i want to redirect on submit which i used to do using
on_sent_ok: "location = 'http://domain.com/about-us/';"
Both would work when used individually.But i want to use both at the same time.
I tried doing
on_sent_ok: "location = 'http://domain.com/about-us/';"
demo_mode: on
Doesnt seem to work. Kindly advice.
The plugin author has changed as of at least 4.0 the way you should do this again. The skip_mail property is now private :
class WPCF7_Submission {
private $skip_mail = false;
...
}
You should use this filter : wpcf7_skip_mail
For example :
function my_skip_mail($f){
$submission = WPCF7_Submission::get_instance();
if(/* YOUR TEST HERE */){
return true; // DO NOT SEND E-MAIL
}
}
add_filter('wpcf7_skip_mail','my_skip_mail');
The author of the plugin Contact Form 7 has refactored some of the code for its version 3.9 and since then the callback function for the hook wpcf7_before_send_mail must be written differently.
To prevent Contact Form 7 from sending the email and force it to redirect after the form has been submitted, please have a look at the following piece of code (for version >= 3.9):
add_action( 'wpcf7_before_send_mail', wpcf7_disablEmailAndRedirect );
function wpcf7_disablEmailAndRedirect( $cf7 ) {
// get the contact form object
$wpcf7 = WPCF7_ContactForm::get_current();
// do not send the email
$wpcf7->skip_mail = true;
// redirect after the form has been submitted
$wpcf7->set_properties( array(
'additional_settings' => "on_sent_ok: \"location.replace('http://example.com//');\"",
) );
}
Hook into wpcf7_before_send_mail instead of using the flag .
add_action("wpcf7_before_send_mail", "wpcf7_disablemail");
function wpcf7_disablemail(&$wpcf7_data) {
// this is just to show you $wpcf7_data and see all the stored data ..!
var_dump($wpcf7_data); // disable this line
// If you want to skip mailing the data..
$wpcf7_data->skip_mail = true;
}
Just an update. The following works in 4.1.1.
on_sent_ok: "location = 'http://domain.com/about-us/';"
demo_mode: on
Set skip_mail: on does the trick.
There might have been a change to contact-form-7, because I wasn't able to access the $skip_mail variable in the WPCF7_Submission object. I looked at the submission.php object in the \wp-content\plugins\contact-form-7\includes\submission.php file and found this:
private $skip_mail = false;
Since the variable is private, and there are no getters or setters in the file, you're not going to be able to change it externally. Just change it to this:
public $skip_mail = false;
and then you can change the variable like this in your functions.php file:
add_filter('wpcf7_before_send_mail', 'wpcf7_custom_form_action_url');
function wpcf7_custom_form_action_url( $form)
{
$submission = WPCF7_Submission::get_instance();
$submission->skip_mail = true;
}
A reminder, if you update the contact-form-7 plugin, it will probably nullify your change, so keep that in mind.
Simple code
Copy and paste the following code in your activated theme functions.php file.
add_filter('wpcf7_skip_mail','__return_true');
UPDATE WPCF7 ver. 7.5: There is now a filter specifically to handle this.
function my_skip_mail($f){
$submission = WPCF7_Submission::get_instance();
$data = $submission->get_posted_data();
if (/* do your testing here*/){
return true; // DO NOT SEND E-MAIL
}
}
add_filter('wpcf7_skip_mail','my_skip_mail');

Global variables vanish in WordPress plugin

I'm writing a WordPress plugin. I want to display a custom message after a post is saved. This message will depend on the outcome of function called when the post is saved.
Here's my code:
add_action('save_post', 'my_save_post_function');
function my_save_post_function() {
global $msg;
$msg = "Foo bar";
...
}
add_filter('post_updated_messages', 'my_post_updated_messages_function');
function my_post_updated_messages_function($messages) {
global $msg;
$messages["post"][1] = $msg; // !! $msg is undefined !!
...
}
Why is $msg undefined?
Is there any way I can get a result out of a save_post action? I've tried all sorts of tricks. Even the $_POST data seems to have been blown away by the time admin messages are shown.
have you tried session ? i think your problem will be fixed .
take a look at :
http://www.php.net/manual/en/function.session-start.php

Categories