Add Page Title to source key in url for use in a form - Wordpress Website - wordpress

I am trying to add a waitlist button functionality to a page on my wordpress website. Basically I need to autopopulate the source key in the query string in a url with the page title.
More Details:
I have the button linking to a general waitlist form but I want the product information from the page title go into a text field. I can do this by using a source key in the url.
We have our product listed on a portfolio post here with a waitlist button: http://www.inventivewebdesign.com/renohifi/listings_portfolio/pass-labs-xa-160-8-monoblock-power-amps/
The button goes to a form here with a text field that auto populates with the source key in the url.
So if I use the link: http://www.inventivewebdesign.com/renohifi/waitlist-request/?source=Pass%20Labs%20XA-160.8%20Monoblock%20Power%20Amps, The source key will populate the the product text field with "Pass Labs XA-160.8 Monoblock Power Amps". This all works great!
Now, I want to automate the code for the button so it always pulls the Page title as the source key so we don't have to manually enter in code for each button.
How can I get the page title to auto-populate the query string in the link url so that the link will be http://www.inventivewebdesign.com/renohifi/waitlist-request/?source={PAGE_TITLE}?
FYI - I am using the Visual Composer plugin for page layout and button creation.
UPDATE:
I am trying to use code like this:
<div class="vc_btn3-container vc_btn3-center">
<a title="Waitlist - <?php the_title_attribute(); ?>" href="http://www.inventivewebdesign.com/renohifi/waitlist-request/?source=<?php echo get_the_title(); ?>">
Add Me to the Waitlist
</a>
</div>
It is in the Wordpress editor so the code is not showing up as anything other than code. I want the title to show (I am trying two different wordpress calls for the page title). You can see this in the 2nd "Add Me to the Wishlist" Button on the first link above.

In JavaScript you can get the page title from the DOM using document.title, the value is encoded using the encodeURIComponent() function.
Your URL as a JavaScript string:
var url = "http://www.inventivewebdesign.com/renohifi/waitlist-request/?source=" + encodeURIComponent(document.title);
I couldn't find any reference to the URL on the page to which you linked.
EDIT: To encode the title string using PHP you need to use the urlencode() function.
Template code:
<div class="vc_btn3-container vc_btn3-center">
<a title="Waitlist - <?php the_title_attribute(); ?>" href="http://www.inventivewebdesign.com/renohifi/waitlist-request/?source=<?php echo urlencode(get_the_title()); ?>">
Add Me to the Waitlist
</a>
</div>

I did it by adding a shortcode instead:
function shortcode_waitlist( $atts ){
$pagetitle = get_the_title();
$link = '<div class="vc_btn3-container vc_btn3-center"><a class="vc_general vc_btn3 vc_btn3-size-lg vc_btn3-shape-rounded vc_btn3-style-modern vc_btn3-block vc_btn3-color-primary" href="http://www.inventivewebdesign.com/renohifi/waitlist-request/?source='.$pagetitle.'" title="Waitlist - '.$pagetitle.'">Add Me to the Waitlist</a></div>';
return $link;
}
add_shortcode( 'waitlist_button', 'shortcode_waitlist' );
Then just used [waitlist_button] in the page.
Thanks for your suggestions, they helped me get to where I needed to go.

Related

How to get a custom value using Contact Form 7 - Dynamic Text Extension

I have a $_SESSION array variable with post ids. Inside foreach loop, I would like to get the posts titles of these ids. Thus so far I have something like this:
sport_title = '';
foreach($_SESSION['sports_post_id'] as $sports_id {
$sport_title = get_the_title($sport_id);
$sports_titles .= $sport_title . "<br />";
}
Now, my problem is that I do not know how to pass it in a custom variable in Contact Form 7 - Dynamic Text Extension plugin.
I have inside my form this field (inside CF7):
[dynamichidden dynamic_sports readonly default:shortcode_attr]
and inside my custom page template php file:
echo do_shortcode('[contact-form-7 id="3561" "CF7_get_custom_field dynamic_sports=\'$sports_titles\'" title="Availability Form EN"]');
Thus, I would like to send these post titles in email.. How can I make it work? thanks in advance
ok I figure it out how to do it! If anyone wants more explanation:
Inside Contact Form 7 - Form tab, I have insert this code:
[dynamichidden dynamic_sports "CF7_GET key='sports_post_id'"]
where key is a standard word (could not change it).
Inside Email tab, you should have this code:
Sports: [dynamic_sports]
Now, inside my Custom template PHP file, I have this shortcode:
echo do_shortcode('[contact-form-7 id="3561" title="Availability Form EN"]');
I also have a form with a hidden input type, with a name sports_post_id and value the id of the current post:
<input type="hidden" value="<?php echo get_the_title( get_the_ID() ); ?>" name="sports_post_id" id="sports_post_id" />
EDITED
Another solution via plugin that extends the CF7, would be the following:
Install Contact Form 7 - Dynamic Text Extension
Copy and paste the form-tag code below and then add it inside the form code block
[dynamichidden page-title "CF7_get_post_var key='title'"]
The above code will add a hidden text input to the form which will pre-populate the page title. This is good to use when you are using the same contact form on multiple pages so you know where the user has submitted the form from. Alternatively, you can display the page URL or slug using one of the below shortcodes instead:
[dynamichidden page-url "CF7_bloginfo show='url'"]
[dynamichidden page-slug "CF7_bloginfo show='url'"]
Displaying the Hidden Dynamic Content Tag Variable in Contact Form 7
Finally, display the hidden dynamic content tag variable in Contact Form 7 form. While you are on the CF7 settings page, click on the "Email" tab and insert this:
[page-title]
If you are using the URL or Slug fields, you these instead:
[page-url]
[page-slug]
In your CF7 Form configuration > Email Tab, you only have to add the desired field between hooks [...]
[dynamic_sports]
This will print the dynamic field value in your email.

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!

Wordpress Images Not Showing - URL for image shows as numbers

Our Wordpress site suddenly stopped showing all 'featured' images. They're just blank. When we look at the source code it shows url(896). We have tried selecting the image again (which shows fine in the backend) but on the frontend it's still blank with an invalid URL. Any idea how to fix that and what may have happened? TIA!
To update: this below code is what's being used to pull the featured images (with the incorrect URL):
<div class="hero homepage relative-block" style="background-image:url(<?php the_field('hero_background_image'); ?>);<?php $hero_aspect_ratio = get_field('hero_aspect_ratio'); if( $hero_aspect_ratio ) { echo "padding-top:".$hero_aspect_ratio."%;"; } ?>"></div>
EDIT:-
The following also isn't grabbing any content. Any ideas?
<div class="page-content">
<?php get_template_part('content-block-loop'); ?>
</div>
Your field hero_background_image returns attachment ID instead of the URL of the image. Use wp_get_attachment_url($id) with get_field() that returns the id, instead of the_field() that outputs the value to get the URL of the attachment/image:
<div class="hero homepage relative-block" style="background-image:url(<?php echo wp_get_attachment_url(get_field('hero_background_image')); ?>);<?php $hero_aspect_ratio = get_field('hero_aspect_ratio'); if( $hero_aspect_ratio ) { echo "padding-top:".$hero_aspect_ratio."%;"; } ?>"></div>
Possible cause: if you use some metabox/custom fields plugin as MetaBox, Toolset Types or the ACF, someone has probably changed the settings for the field, to return the ID of the attachment instead of the URL. So that might also solve the issue.

Wordpress / WooCommerce - dynamic hyperlink based on current product

I'm running WooCommerce in Wordpress here and on some products, I will be embedding a block of text which includes a hyperlink to a contact form. I would like the Subject on the contact form to populate with the name of the product where the hyperlink was clicked
I have established that I can populate a CF7 field by appending a query string to the URL (e.g. www...co.uk/contact-form/?subject=blah,blah), so if there was some method of dynamically altering the hyperlink in the aforementioned block of text when the product page is loaded so as to append the product name after the query string
Maybe to describe this a little clearer...
The block of text on the product page will look like this:
<a href='...co.uk/contact-form/?subject={NEED-TO-DYNAMICALLY-APPEND-PRODUCT-NAME-HERE}>Contact us...</a>
Any ideas folks of how I might approach this or has anyone tried something similar?
Thanks
So you want the title of product name ?
$product = wc_get_product( id );
$product_name = $product->get_title();
<a href='...co.uk/contact-form/?subject=<?php echo $product_name; ?>'>Contact us...</a>
If you want the title of the page you can give this parameter to your href
$pageTitle = get_the_title();

Wordpress Shortcode Displays as is on the Page

I am using a Portfolio Plugin. And it asks me to add the following shortcode in to the page where i want to show the content
[nimble-portfolio template="2colmosaic" types="3,4,5" items_per_page="3"]
When i do the following
$view = get_page_by_title('View');
echo $view->post_content;
It displays
[nimble-portfolio template="2colmosaic" types="3,4,5" items_per_page="3"]
as text when rendered
Edit
I tried to do the following
echo apply_filter('the_content', $view->post_content);
But it doesn't Render the full page now. And stops upto that point.
Try this following code and let me know the case then -
<?php echo do_shortcode($view->post_content); ?>

Resources