Wordpress Gravity Forms Dynamic passing of id of form - php

I can hardcode the shortcode for Id in template but I read that this is not a good practice, my question is how to dinamicly pass Id of form so that one day when user needs a new form he can change it without calling a developer(me)?
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php echo do_shortcode("[gravityform id='1' title='true' description='true']"); ?>
<?php endwhile; else : ?>

Put it in an (advanced) custom field, or simply in the content.

Use Advanced Custom Fields and create an Options page where the shortcode can be edited, examples are available here:
https://www.advancedcustomfields.com/resources/options-page/
Then in your theme you put something like the_field('contact_form, 'option'); instead of the echo do_shortcode(...); part you have now. Afterwards the user can edit the field and insert a new form.

If you're already using ACF or are open to using it, you might consider this plugin which will allow the user to select from a list of Gravity Forms:
https://github.com/stormuk/Gravity-Forms-ACF-Field

Related

$variable = get_post_meta not working as expected

This is my first post on stackoverflow, as I am quite new to PHP. I am learning the language to help me customize my online portfolio in Wordpress, and I usually manage to make the changes I need - but not this time, apparently.
I am trying to use get_post_meta to read a meta tag in my portfolio pages, and avoid displaying the page thumbnail. This is the code I am using:
<?php $disable_thumb = get_post_meta( get_the_ID(), 'minimal_portfolio_page_thumb', true );
if( $disable_thumb !== 'on' ): ?>
<?php if ( has_post_thumbnail() ) : ?>
<div class="post-thumbnail">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(); ?>
</a>
</div>
<?php endif; ?>
<?php endif; ?>
Using a meta tag management plugin, I added the following tag to all my "portfolio"-type pages:
<meta name="minimal_portfolio_page_thumb" content="on">
I am currently checking if this works in this page of my web: egozalor.com/portfolio/hansel-gretel/
Long story short, the trick does not work as expected. I guess there is something I am doing wrong or not realizing, due to my little knowledge of PHP. Any indications, tips or recommendations are very welcome!
Also please let me know if further or more specific information is necessary to assess my issue.
Thanks in advance!
The function get_post_meta has nothing to do with the <meta> elements of your site. With this function you can only get metadata of the post itself. You can set the metadata on each post at the end (custom fields) as key / value pairs.
These custom fields are not visible on the site itself. You can create a custom field on every post with key minimal_portfolio_page_thumb and value on (or another value like 0/1).
Looks like the custom fields are disabled by default on WordPress. But you can enable the custom fields without an additional plugin. On the top right of your post you can find three dots to open a menu. At the end of the menu there is the entry "Options". On the options you can enable the custom fields.
You can enable the custom fields for posts and pages.

Wordpress template Echo a php string

I'm working on a page template and I'm having trouble working out how I can echo a string of php. Basically I have set up a custom field inside Wordpress for the page called Slider Code, and it asks the user to paste in the slider code given to them so that the appropriate slider is displayed on the page. The slider code given looks like this: [soliloquy id="82"]
The custom field plugin gives me this code to use in the template to echo out whatever is in that field, which looks like this: <?php echo CFS()->get('slider_code'); ?>
In WordPress there is a php function called do_shortcode which enables me to use that code, so it would look like this: <?php do_shortcode('[soliloquy id="82"]'); ?> and the slider would be displayed. So is there any way I can merge that <?php echo CFS()->get('slider_code'); ?> string with the do_Shortcode string? If not, does anyone have any alternative solutions to this issue? I can only think of having to make separate page templates for each individual slider.
EDIT: I have just discovered that the do_shortcode function does not actually work at all with the soliloquy shortcode. It does give me the option of using this code: if ( function_exists( 'soliloquy' ) ) { soliloquy( '82' ); }. However, when I paste this code into the custom field as is, the <?php echo CFS()->get('slider_code'); ?> tag just echos out that text. If I wrap the code in the custom field in <?php and ?> it just comments the code out inside my inspector. How do I get around this?
Check this out.
$slider = CFS()->get('slider_code');
call_user_func('do_shortcode', $slider);
I don't understand why you can't just run do_shortcode(CFS()->get('slider_code')).
Ok. Then try this one. Paste this code in the template file.
<?php
if ( function_exists( 'soliloquy' ) ) {
soliloquy( CFS()->get('slider_code') );
}
?>
And for slider_code input just enter '82'. If you are unable to see what is the template file you have to paste the above given code, then try to see WordPress template conventions.

Insert PHP code in shortcode using do_shortcode()

I am trying to insert PHP code in a shortcode.
For now it look likes this:
<?php echo do_shortcode('[to_like][/to_like]'); ?>
<?php do_action('single_spot_after_content', get_the_ID(), 'after_content'); ?>
I have tried several plugins to combine this to one shortcode (PHP shortcode plugin, Advanced Custom fields, etc.)
This is what I want to accomplish, but it is not working:
<?php echo do_shortcode('[to_like]<?php do_action('single_spot_after_content', get_the_ID(), 'after_content'); ?>
[/to_like]'); ?>
In the front-end the user can select the value through a dropdown field --> for example 10% discount (this is going to be displayed when a visitor likes the box). The theme has a special markup field (you can create custom submission fields) where %% is going to be changed by the chosen value from the customer. This value is going to be displayed on the website (singe listing page).
How can I implement this? This is so vital for the website.
Try this:
<?php echo do_shortcode( '[to_like]' . do_action('single_spot_after_content', get_the_ID(), 'after_content') . '[/to_like]' ); ?>

Custom link and name meta box in wordpress

I would like to create a metabox in the section of edit post menu. Like Tags for exemple, BUT instead of inserting tags I would like to put a link (ex. http://www.google.com) + name.
This link should go right to the end of my post content as a hyperlink with "Source: Name (hyperlinked by specified link)
Here is an example of what I am needing:
Title
This post is for example
Source: Google
Any help would be much appreciated. Thanks guys.
You can use the Custom Fields in Wordpress posts to add extre information to a post, then in your theme you can just grab the values. Here is what I use on my blog:
$custom_fields = get_post_custom($post_id);
if(!empty($custom_fields['article_source_url'][0]) && !empty($custom_fields['article_source_title'][0])){ ?>
<strong>Source</strong>: <?php echo($custom_fields['article_source_title'][0]); ?>
<?php }
Then in your wordpress post just add a Source URL and Title in the custom fields box:
You can use WP's Custom Fields to denote specific parameters for each page/post.
Then, in the template, just call them like this:
<?php
$custom_fields = get_post_custom();
var_dump($custom_fields);
?>
Note that they are arrays, so in case you have a field named URL, you'd need to call it like
echo $custom_fields['URL'][0];
This is done like this so you can have multiple values for the same field.
Also, in order to see the Custom Fields box, you need to go to the Screen Options (top right of the edit page) and enable them.

Display posts on tag.php template?

Is it possible to create a tag.php template that when you navigate to the url www.domain.com/tag/architecture/ it will display all the custom posts that have been tagged with that specific tag? And so on for various other tags?
What code will I need to include on my template?
Yes You can create,below is the code i used to display custom post type "knowledge"
<?php
global $query_string;
$posts = query_posts($query_string.'&post_type=knowledge');
if ( have_posts() ) while ( have_posts() ) : the_post();
?>
<?php the_title(); ?>
<?php endwhile; // end of the loop. ?>
This will help you understand hierarchy of templates:
http://codex.wordpress.org/images/1/18/Template_Hierarchy.png
Use of $query_string (example) available here:
https://developer.wordpress.org/reference/functions/query_posts/
According to the template hierarchy, you can create a file called tag.php which will be used instead of index.php if a tag page is displayed. You can also prepare separate templates for specific tags.
The best way is to start by creating a copy of your theme's index.php and calling it tag.php, this way you'll have some basic code working. Then you can modify tag.php to fit your needs - probably by editing The Loop, or maybe changing some includes if your theme loads the loop from separate file. (but in this case you should consider editing index.php to load some other loop for tag pages - is_tag() may come in handy)

Categories