Skip Navigation

[Resolved] Using cred_delete_post_link in PHP but NOT with echo

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

Problem:

The issue here is that the user wanted to render the delete link for a post using php.

Solution:

This can be done by having a look at my post below.
https://toolset.com/forums/topic/using-cred_delete_post_link-in-php-but-not-with-echo/#post-1244786

This support ticket is created 5 years, 7 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 10 replies, has 2 voices.

Last updated by alexG-4 5 years, 7 months ago.

Assisted by: Shane.

Author
Posts
#1244699

I need to display a list of "delete" links for all child posts of a relationship. I'm doing this from within some nested Views.

I tried to create a View that would cycle through all the children, and call the [cred_delete_post_link] shortcode for each one.

This worked in principle, but unfortunately, because my setup is a bit complex, the call to that View was too complex and I couldn't get the View's shortcode to be parsed.

SO - I've written my own shortcode to use instead of that View. This works fine until it comes to actually displaying the delete links.

I found from the usage example here
https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_delete_post_link
that I can use the cred_delete_post_link() function, but ONLY via echo. But that's not going to work for me because the output appears in the wrong place when called from within a View.

It seems that I need to construct a string that includes all the delete links, and return that as the result of my shortcode.

Is there any way to do this?

Alex

#1244708

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Alex,

Thank you for getting in touch.

You can try using return instead of echo and let me know if this helps.

Thanks,
Shane

#1244752

I don't see how that would work.

For each child, I want to display:

PostTitle (Remove)

where Remove is hyperlinked with the delete link.

My shortcode displays ALL the children, so I'm constructing a string with all the all the entries and returning the string.

I don't another way of doing it.

Alex

#1244778

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Alex,

Seems like it will be in a loop.

I mean the only other option I can think of that might work is to use the actual shortcode in your template file by using the do_shortcode function but this itself needs an echo.

Maybe you can construct everything as a large string and just return the entire string of content all at once. Just letting you have some Ideas that i'm thinking.

Since you are doing a loop the return would definitely not work.

Thanks,
Shane

#1244785

Hi Shane

You suggested
"Maybe you can construct everything as a large string and just return the entire string of content all at once. Just letting you have some Ideas that i'm thinking."

That's exactly what I'm doing, since it seemed like the only option.

Within my shortcode code, I have this loop:

  foreach ($relsArray as &$relItem) {
    $childPostID = $relItem;
    $relItem = "<a href=" . get_post_permalink( $relItem ) . ">" . get_the_title($childPostID) . "</a> (" . cred_delete_post_link($childPostID) . ")";
  }

$relsArray is an array containing the IDs of all the child posts.

After adjusting that array via that loop, I "implode" it to create a string, and return that string.

It all works fine, except nothing appears in the ( ) because cred_delete_post() doesn't return anything.

That's my dilemma!

Alex

#1244786

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Alex,

I think its cause you are not providing the other parameters except the ID. I'm not seeing a display text.

echo cred_delete_post_link(get_the_ID(), 'Delete this post', 'delete', 'cred-refresh-after-delete my-custom-css-class', 'display: block;', 'Do you really want to delete this post?', 'Post Deleted.', 1, 1);

Take a look at the example above. You need to provide it with some more parameters like the text and action.

cred_delete_post_link($post_id, $text, $action, $class, $style, $message, $message_after, $message_show, $redirect);
 

Please try and let me know if it helps.
Thanks,
Shane

#1244789

Thanks, Shane - progress of sorts...

I'd not considered including more arguments because the arguments are shown as optional in the documentation. I guess that applies to the shortcode, but not the function.

So, the progress is that the output is now being generated.

The problem now is that it's behaving like echo! All the "Delete this post" links appear at the top of the page rather than within the string I'm trying to construct.

It's a bit of a quirk for such a function - unless there's a reason for it that I haven't figured out.

Alex

#1244791

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Alex,

I agree as well.

It leaves us with the option to try using the do_shortcode function to render the shortcode in your string and store it in the $relItem variable.

Try using the do_shortcode function on the delete link shortcode itself.

Thanks,
Shane

#1244807

Hi Shane

That was the trick that I was missing! Thanks for the suggestion.

In case others have the same problem, this is the code I ended up with

  foreach ($relsArray as &$relItem) {
    $childPostID = $relItem;
    $shortCodeString = "[cred_delete_post_link post='" . $childPostID . "' text='(Disconnect this)' action='delete' message_show='0' ]";
    $relItem = "<a href=" . get_post_permalink( $relItem ) . ">" . get_the_title($childPostID) . "</a>" . do_shortcode($shortCodeString);
  }

  return  "<pre>".implode(PHP_EOL,$relsArray)."</pre>"; 
 

with $relsArray starting off as an array of the IDs of the posts whose delete links are to be displayed.

The only quirks are:

1. A newline gets displayed before and after the shortcode (annoying but not disastrous), and

2. I still need to figure how to do something sensible after the user clicks. Currently, the link just changes to
"Deleting....." and stays like that. I'll either have to feed in the ID of the current page, and provide that as the redirect page (an optional shortcode attribute), or perhaps do something clever with Ajax.

But the core problem is solved.

Thanks!

Alex

#1244810

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Alex,

Happy to see that we are making progress.

The CRED delete shortcode also has a parameter for redirect, so you can provide the id there.
https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_delete_post_link

"1. A newline gets displayed before and after the shortcode (annoying but not disastrous),"

I think this is because its creating it as a block level html code which I think can be fixed with some css manipulation.

Thanks,
Shane

#1244814

Yep - I'm sure it can all be done. In fact, I've fixed that newline issue with css before, so I'll just find out what I did then.

Thanks, Shane.

My issue is resolved now. Thank you!