Skip Navigation

[Resolved] Assign a Layout to a Post after submitting by CRED

This thread is resolved. Here is a description of the problem and solution.

Problem:
How can I assing a Layout to a Post programmatically after submitting the new post with CRED?

Solution:
That is a Custom Code solution.
You shopuld study the CRED API and then modify this example accordingly to your site:
add_action('cred_save_data', 'my_save_data_action',10,2);
//This is our function

function my_save_data_action($post_id, $form_data)
{
    // Change the below Number to the ID of the CRED Form you use (change 12 to the ID of your Form)
    if ($form_data['id']==12) 
    {
        //Below we use update_post_meta to update the field _layouts_template with the slug of the layout to be assigned to the post.
        update_post_meta($post_id, '_layouts_template', 'the-slug-of-the-layout-here' );
    }
};

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/

This support ticket is created 7 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 11 replies, has 2 voices.

Last updated by Bochnacki 7 years, 5 months ago.

Assisted by: Beda.

Author
Posts
#529189
activation-error.jpg

Activation error of post paid within the Package.
The new post added within the Package is not automatically activated. You have to do it manually.
It appears in the results list or in the list of posts in the category, but after clicking to see the details of the post, the page is empty - the post layout is not displayed.

Posts added outside of the Package are displayed well, both in the results list and after the opening. You do not have to manually activate the admin panel.

Please help. What could be the cause and how to fix it? What to do to post added within the package activated automatically?

#529346

I am confused with "activate the admin panel".

Toolset does not feature any activation of an admin panel; I assume you talk about something else.

From your post, I think it could be related to the Layout being assigned to the post.

Could you elaborate what you exactly do, and what you expect to be happening?

If you have a layout that should be assigned automatically to new Posts of a particular kind, you need to follow these steps:

1. Create the Layout
2. In the Settings "Change how this Layout is used" and then "Template for multiple pages:", choose your post type.
3. Save

This will not only assign the layout to all posts, but also to all new (future) posts.

Can you elaborate on the exact steps to replicate the error, in case that does not work on your end?

#529374

Sorry, my mistake, it should be - activate in the admin panel.

"1. Create the Layout
2. In the Settings "Change how this Layout is used" and then "Template for multiple pages:", choose your post type.
3. Save "
Exactly so I have done and it works well.
The problem is only when the post is added in the Package.

It looks like:
I buy a Package for 5 posts -> I add a new post within the Package, and in the Package there are 4 posts to add -> new post is in the list of results -> click on this post to open it -> opens blank template - ERROR
I add a new post outside the Package -> new post appears in the list of results -> click on this post to open it -> opens the template with details of the post - WELL

I hope this is understandable now 🙂

#529377

Yes, I think I got it, although this sounds rather impossible to me :D.
(But given your report, there is nothing to doubt 🙂 ).

If the Layout is set to be assigned it should be used.
It's a simple Custom Field (a hidden one) that makes this happen.
We could work around with some Custom PHP, in case we cannot solve it on the first shot on the site you use.

The idea would be to apply a cred_save_data() action on form submit, that updates the Posts Field "_layouts_template" with the Slug of the Layout you want to be used.
This is a simple update_post_meta() inside of the cred_save_data()
https://codex.wordpress.org/Function_Reference/update_post_meta
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

To solve this problem without a workaround, I think the best is if you can send me a copy of the site, with exact details where I can see the issue and how to reach it.

I will then try to solve that, or at least find out why it happens, so to provide a Bug Solution.

These are the instructions on how you can provide me with a Site's Snapshot.
https://toolset.com/faq/provide-supporters-copy-site/

#529404

I only received the Database and installer.

Can you also share the archive with me?

I tried to get them from the FTP but as well there, the whole archive is missing.

Duplicator will enable you to create a package.
I think you did that already as you sent me the installer

But it also creates an Archive (this are all the FTP Files you use in this install)
Can you share that with me as well?

You can use the Google Drive Service for that, sharing with anyone who has the link.

#529641

The link does not hold the required Archive.

I will try to get it myself from the site.

Meanwhile below a code to solve the problem.
It is to be used in the functions.php file of your Theme, and you should edit the CRED Form ID in the Code (see comments).

I am not sure wether you use a Payment form, in that case, you need to hook in with the CRED Commerce API eventually.
The code is the same, just the wrapping CRED API hook changes.

//This hook fires when the post is saved in the database
//https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
//If you use a CRED Commerce form, you perhaps should use one of this hooks instead:
//https://toolset.com/documentation/programmer-reference/cred-commerce-api/
add_action('cred_save_data', 'my_save_data_action',10,2);

//This is our function
function my_save_data_action($post_id, $form_data)
{
    // Change the below Number to the ID of the CRED Form you use (change 12 to the ID of your Form)
    if ($form_data['id']==12) 
    {
        //Below we use update_post_meta to update the field _layouts_template with the slug of the layout to be assigned to the post.
        update_post_meta($post_id, '_layouts_template', 'the-slug-of-the-layout-here' )
    }
}

That's all. This should solve the issue at least temporarily.
Please make sure to test the code in a safe environment first, or to backup the live site at least before you apply it.

Meanwhile I will debug the issue.

#529645

It interrupts the build.

Please, can you send me the archive.zip file?

Thank you

#529723

I am rebuilding the site using the Database and adapting the FTP to what you have.

I'll be back in an hour with some news, hopefully useful ones.

#529736

OK, there is a confusion here.

1. You mention to create a post in "Package"
2. But that Post type has no layout applied at all
3. In your previous description once you also mention, to use "NOWE OGŁOSZENIE"".
OGŁOSZENIE is a different post type, not a "Package".
4. The Layout, you mention is "Ogłoszenie - osobowe" but that does not apply neither to Packages, nor to Ogloszenia post type.

Actually, there are no Layouts at all applying either to Packages or Ogloszenia Post Type.

All you would need to do is assign the proper layout to the proper Post type, unless I confuse your name-conventions, or the steps.

I apologise if I did misunderstand.

#529833

Yes, my website is structured differently than the reference "Classifieds".
But I see that my post layout is done just like the "Classifieds".
"Single - Ad" is assigned to "Listing" = "Ogłoszenie - osobowe" is assigned to "Osobowe".
"Listing" is the same CTP as my "Osobowe".

"Actually, there are no layouts at all, either to Packages or Post Offices." - My CPT "Ogłoszenia" is not the same as "Listing". It only serves to get the appropriate address for each category.

"All you would need is to assign the proper layout to the proper post type, unless I confuse your name-conventions, or the steps." - I do not know what I would improve / change ...

Anyway, now I see another problem related to making different packages for different categories. It is very possible that the current structure will change.
May we not close this topic for a while and return to it in a few days?

#530739

I did not relate to the Classifieds Reference site at all.

What I say, is your initial report and your subsequent reports talk about different Post Types.
Now you also added "Single Ad."
Then you mention that "Single - Ad" is assigned to "Listing" = "Ogłoszenie - osobowe" is assigned to "Osobowe".
"Listing" is the same CTP as my "Osobowe".
This is not possible.
Either Listing is the same as Osobowe, or it's assigned to Osobowe.
Both is not possible

Furthermore, your initial report talks about a post type Package, OGŁOSZENIE.

There are no Layouts at all applying either to Packages or Ogloszenia Post Type.

What I need from you is a clear path to:

1. What post type is created/edited with CRED
2. Where is that CRED Form
3. Where is the Layout, that should be assigned to (what) post

Then I can show you what you need to do, although all it needs, is the Layout being assigned by default to that Post Type.
Unless there is a BUG, which I cannot spot until now.

You can leave this ticket open up to 30 days from now on, and a Clean Up Robot will remind you about it.

If the issue changes or structure changes, please open a new ticket, so it's clear from the begin.

Thank you

#532160

Beda, your code is good 🙂 Only ran out of semicolon ";" 🙂

add_action('cred_save_data', 'my_save_data_action',10,2);
//This is our function
function my_save_data_action($post_id, $form_data)
{
    // Change the below Number to the ID of the CRED Form you use (change 12 to the ID of your Form)
    if ($form_data['id']==12) 
    {
        //Below we use update_post_meta to update the field _layouts_template with the slug of the layout to be assigned to the post.
        update_post_meta($post_id, '_layouts_template', 'the-slug-of-the-layout-here' );
    }
};

Now Layout is immediately published, you do not need to activate it manually.
Thanks a lot! 🙂