Passing a Wordpress ShortCode attribute value to a template - wordpress

I wrote a custom post type to centrally manage lead forms, this has an accompanying short code so a lead form can be added to a page using [lead-form id="574"] - in this example, 574 is the Id of the lead form. When this is added to a page, I use a specific template for landing pages that include lead forms.
There are the fields in the custom post type that I need to access in the template, but in order to access them, I need the Id of the lead form first. I have included some screenshots that I hope will clarify this a little better.
Lead forms are centrally managed in a custom post type, in this example, 574 is the Id of the custom post
The code for the shordcode grabs the id using while( have_rows('pages', $atts['id']) ): the_row(); The fields used for the custom post type are a repeater of pages, which has a repeater of questions which has a repeater of predefined choices. These fields are created using the Advanced Custom Fields plugin. The shortcode is basically looping through pages/questions/answers and building a string that the shortcode returns. When adding a short code to a page, I specify a template in the "Page Attributes" section so I can include some styles and javascript.
template-lead-form.php is the file used for the "LP Lead Form" The string that is built in the shortcode is outputted in the_content(), however, there are two fields that I need access to that I can only get by passing in the Id to the lead form.

I'd search for your shortcode in post_content and get id from there. Regex isn't my strong side, but probably something like this:
preg_match('/\[load-form [^\]]*id="([0-9]*)"\]/', $post->post_content, $res);
After that you'd have id in $res[1].

Related

Can I set a custom link on a custom post type in WordPress?

I am creating custom post types in WordPress using the "Types" plugin. Those post types are not supposed to have a post page associated with them that the user would navigate to by default when clicking a particular post link. They are supposed to be merely titles that I am displaying in a grid and that should link out to separate sites. Every post title has an individual link associated with it. I am styling those post titles using the "Grid Element" creator of the "Visual Composer" plugin, so I can set which attributes of the post I want to display (in my case only the title).
When I am creating the post type, I am adding a custom field for the link url that I can then set when I create the individual post. However, I am not seeing an option to set that custom link to be the link for the post itself. I can add the link as a separate UI element of the post but I want the post title itself to be the link. Is this possible and how?
Very possible, but you will have to do some coding of your own, rather than relying on visual composer. Within the template you are using, in the loop that pulls the posts, you need to grab the meta data from that field for the current post:
<?php
// loop stuff here...
$link = get_post_meta(get_the_id(), your-key-here, true);
echo '<p>'.the_title().'</p>';
// other loop stuff...
?>
In the above, your-key-here refers to the name of the custom field you've set up for the link that gets set by the post author. The above also assumes you know enough about WordPress to know where you need to edit this code in, in the template. The code does not check for empty values and handle with a fallback, so yes, this code can be improved upon, but it sill do what you've asked in the question.
EDIT: A link for reference regarding pulling meta data/custom fields data; https://developer.wordpress.org/reference/functions/get_post_meta/

Wordpress custom post type structure

i am building a wordpress website for a client. i need to build 1 template type of page with a list of fields that the client can fill in to populate the page. so lets say i build a template page called Person Template. On it i have an empty picture placeholder on the left and an empty Name Field below that. On the right i have an empty "persons phone number" and "Persons email" field.
what i need to create somehow is a system that the client can enter these 4 items into wp-admin somewhere and save that as a new Person. Then they can repeat and save another Person until they have created say 100 Person pages.
How do I do this? I tried building a Custom Post Type called Person CPT. I have built a Page called Person Template. I have built some "Person CPT" posts and put them on a page but i dont know how to create the Entry Fields page for my client to use.
You're correct that you'll need to use a custom post type. To add the fields you have two options:
Add the fields using add_meta_box(). You'll then need to save this data as post meta. Then in your single-person.php template you would retrieve the meta using get_post_meta(). It's a tad cumbersome, as you need to write the markup for the meta box, hook it into an action to add it to the admin page, and verify the content before it's saved. Here's an article that walks you through it
Alternatively get yourself the Advanced Custom Fields plugin: it makes adding additional fields very quick and easy. I use it all the time.

WordPress custom post type archive with description

I have a common design pattern I'm not entirely sure how to best execute in WordPress. The layout is a listing of post teasers (title, trimmed body, image) on an overview page. Let's say example.com is a boating safety company, and at example.com/classes there's a listing of their posts of the class type using archive.php or archive-$posttype.php
So far it's dead simple and default WordPress behaviour. But, I want to have some intro information about this type of information in general on this overview page. Furthermore, let's say I have 10 custom post types hypothetically, and each one would follow this pattern with a listing and a general introductory paragraph on the archive page.
What I don't want to do is have a page for each of these types, and a custom query done for each of them in a page template. If there were a way to associate a description, and even better custom fields, with the post type itself (not on posts of that type but the type itself) that would be the ideal scenario. Ideally I'd like to have this all wrapped into my archive.php template.
Does anybody know of a good way to pull this off?
This may or may not be helpful, but I'll be creating post types in code but using Advanced Custom Fields for the custom fields themselves.
read up on creating post types there is a option for has archive, set it to no, it wont create the standard urls for list view etc. You can modify archive.php to include all post types as well.
I think you are confused about the way wordpress custom post types exist on your server. Basically they only exist in php code, so everytime the wp system loads, your custom code runs to create the custom post type...you are basically setting the rules of the post type at this stage -- so
Custom post type name
Arguments for post type (google create post type)
Custom taxonomies
Meta boxes for admin screen (recommended instead of using custom fields...)
Wordpress if it sees the posttype is public & has_archive will resolve any url requests for the post-type with the appropriate template...for example a post type of "mypost". In this case typing www.example.com/mypost will return back the archive view from your theme (archive.php or archive-mypost.php). This file is located in your theme and you can modify it to include a archive of all post types (replace get_posts with wp_query for total control over posts)
On these archive pages you will see a function called get_posts / wp_query and then standard loop structure to loop through each item. These functions query the wp_posts table in the database to return rows with the post_type you are querying (asking the function to find)(see the DB and the column post_type in wp_posts). There is also a secondary table to store unlimited extra fields (dont take me literally there, but in theory & memory constraints pushed aside, there is no limit set by WP on the extra fields) in a table called post_meta which uses the post id (unique identifer of the row in wp_posts). You can display this information by using get_post_meta() (google it)
So where does that leave you assuming there is a theme installed?
(prob best to create a child theme here)
create your custom post types in the functions file with a have_archive value of false in the arguments array
Add meta box function for the custom fields you want in your functions file (google this, very easy to do)
Modify your archive.php to run a new query for all post types (google wp_query, you can query a array of post types)
Any other info like descriptions, default save behaviour can be set using php in your functions file and hooked to the action you want to modify (eg save post, etc).
register_post_type() has a description arg which defaults to an empty string. You can use that and get the description like e.g.:
$obj = get_post_type_object( 'my_post_type' );
echo $obj->description;
If i understand correctly, you just need a different description loaded for each post type but maintaining a single archive.php, basically you just need to display the description like so:
<?php
if (get_the_post_type_description()) {
echo get_the_post_type_description();
}
?>

Wordpress custom post type page

When creating a custom post type and fill it with some posts wordpress generates a page for that custom post type.
But I want to be able to add content to that page so I create a regular page and choose a template where I manually call the custom post type posts and loop them out. And now I can just add content in the wysiwyg editor. But this causes a conflict between these two pages, especially if the CPT has the same name as the page.
And now the question: Is there a way to always show the page where I have chosen the template for the CPT to always show? Even when someone try's to manually enter the url for the CPT generated page?
Here is what I understood from your question. You have custom post type. Then you created a page-template, that loops through your custom post types and output them. Now when you create a new page and assign it the page-template you have created you run into problems, if it has the same name as the custom post type.
Solution: I think what you need to do is change the custom-page slug from the admin menu. Then you would be able to directly reference it by the URL.

Wordpress: How to create a dropdown for users to sort posts by date, title, or custom field

I have a site for a client and one page displays posts from a custom post type. Its a record label and I created a custom post type for "releases".
A page displays all releases. I need to create a dropdown menu though that allows people to alter the display of the posts by Title, Date (post's date serves as "release date"), or Artist (custom field).
How would I code the dropdown?
You would create a <select> input directly in your template for that page, assuming you have one (create one if you don't). You can then either use a <form action="" method="POST"> wrapping the dropdown or some JavaScript to submit the form to itself (that is, action="?sort" or similar to submit back into your template). Both of those are fairly simple to implement, but just say so if you're unsure.
Then, in the same template file, before your WP_Query or query_posts (or get_posts, etc), grab the POST variable: $_POST['dropdown_name'] and use an if elseif else or switch() statement to create your post query depending on that POST variable.
This is the easiest approach. Not sure what your level of experience is, say so if you got lost.

Resources