Modifying plural form of views_edit in wordpress - php

I want to change default Draft text for my custom post type views, and I found a function that could do this:
add_filter( 'views_edit-custom_post_type_name', 'custom_draft_name', 10, 1);
if (!function_exists('custom_draft_name')) {
function custom_draft_name( $views ) {
if (isset($views['draft']) && $views['draft'] != '') {
$views['draft'] = str_replace(esc_html__('Draft', 'custom_post_type_name'), esc_html__('Unapproved', 'custom_post_type_name'), $views['draft']);
print_r($draft_array);
}
return $views;
}
}
The problem is that I get only singular change for this. So if I had one Draft I'll have one Unapproved. But when I have more than one I'll have two Drafts and automatically I'll have two Unapproveds. Which is terrible. It should be two unapproved. So I tried to use _n() for the plurals, by creating an array to check against with my str_replace:
add_filter( 'views_edit-custom_post_type_name', 'custom_draft_name', 10, 1);
if (!function_exists('custom_draft_name')) {
function custom_draft_name( $views ) {
if (isset($views['draft']) && $views['draft'] != '') {
$draft_array = array(esc_html__('Draft', 'custom_post_type_name'), _n('Draft', 'Drafts' , 10, 'custom_post_type_name'));
$views['draft'] = str_replace($draft_array, esc_html__('Unapproved', 'custom_post_type_name'), $views['draft']);
print_r($draft_array);
}
return $views;
}
}
But I still get Unapproveds instead Unapproved.
Any help how to solve this?
** ANSWER **
And once again I found the solution immediately after I post on SO xD
Just do a wp_count_posts('custom_post_type') like
$custom_posts = wp_count_posts('custom_post_type_name');
if($custom_posts->draft == 1){
$views['draft'] = str_replace(esc_html__('Draft', 'custom_post_type_name'), esc_html__('Unapproved', 'custom_post_type_name'), $views['draft']);
} else{
$views['draft'] = str_replace(esc_html__('Drafts', 'custom_post_type_name'), esc_html__('Unapproved', 'custom_post_type_name'), $views['draft']);
}

Related

Get original URL referer with the condition

I need to collect data through a contact form and track customer referrals. I am creating a tag for the form, but I need it to be filled only if there was a transition from a referral that contained partner
my code
// Original Referrer
function wpshore_set_session_values()
{
if (!session_id())
{
session_start();
}
if (!isset($_SESSION['OriginalRef']))
{
$_SESSION['OriginalRef'] = $_SERVER['HTTP_REFERER'];
}
if (!isset($_SESSION['LandingPage']))
{
$_SESSION['LandingPage'] = $_SERVER["REQUEST_URI"];
}
}
add_action('init', 'wpshore_set_session_values');
///
function getRefererPage3( $form_tag ){
$partner = array (
'/partner/',
);
$parts = parse_url($_SERVER['HTTP_REFERER'];
if (!empty($parts['path']) || in_array($parts['path'], $partner)){ //this row. I doubt how it should be
if ( $form_tag['name'] == 'referer-page3' ) {
$form_tag['values'][] = $_SESSION['LandingPage'];
}
}
return $form_tag;
}
if ( !is_admin() ) {
add_filter( 'wpcf7_form_tag', 'getRefererPage3' );
}
I found my problem. Maybe this solution will help anyone in the future.
function getRefererPage3( $form_tag ){
if (strpos($_SESSION['LandingPage'], 'partner') !== false) {
if ( $form_tag['name'] == 'referer-page3 ) {
$form_tag['values'][] = $_SESSION['LandingPage'];
}
}
return $form_tag;
}
if ( !is_admin() ) {
add_filter( 'wpcf7_form_tag', 'getRefererPage3' );
}

Wordpress Contact Form 7 custom validation rule not executed

Can't seem to get add_filter() to get working to add a custom validation rule to contact form 7 (v5.2.1). Tried many examples that claim to work from this website and other webs. But non seem to work so far. Can anyone shed some light on this please.
Tried below code.
function custom_text_validation_filter($result, $tag) {
$type = $tag['type'];
$name = $tag['name'];
if($name == 'your-subject') {
$value = $_POST[$name];
if (preg_match('/[\'^£$%&*()}{#~><>|=_+¬]/', $value)){
// $result->invalidate( $tag, "Invalid characters." ); // this did not work
$result['valid'] = false;
$result['reason'][$name] = 'Invalid characters';
}
}
return $result;
}
add_filter('wpcf7_validate_text','custom_text_validation_filter', 999, 2);
add_filter('wpcf7_validate_text*', 'custom_text_validation_filter', 999, 2);
Since above code does not work, tried following.
function custom_text_validation_filter($result, $tag) {
$type = $tag['type'];
$name = $tag['name'];
if($name == 'your-subject') {
$value = $_POST[$name];
if (preg_match('/[\'^£$%&*()}{#~><>|=_+¬]/', $value)){
// $result->invalidate( $tag, "Invalid characters." ); // this did not work
$result['valid'] = false;
$result['reason'][$name] = 'Invalid characters';
}
}
//Added below lines to fire validation error no matter what. But still contact form submits successfully.
$result['valid'] = false;
$result['reason'][$name] = 'Invalid characters';
return $result;
}
add_filter('wpcf7_validate_text','custom_text_validation_filter', 999, 2);
add_filter('wpcf7_validate_text*', 'custom_text_validation_filter', 999, 2);
Further tried different priorities to add_filter too.
add_filter('wpcf7_validate_text','custom_text_validation_filter', 1, 2);
add_filter('wpcf7_validate_text','custom_text_validation_filter', 2, 2);
add_filter('wpcf7_validate_text','custom_text_validation_filter', 10, 2);
add_filter('wpcf7_validate_text','custom_text_validation_filter', 20, 2);
add_filter('wpcf7_validate_text','custom_text_validation_filter', 999, 2);
Non worked
Are you doing well in your function?
The parameter $tag is an object, it seems to me.
Other than that, I don't see what is blocking :/
https://contactform7.com/2015/03/28/custom-validation/

Generate meta page title exactly as on h1 tag

Site based on Joomla. I have many pages where h1 header is mentioned as product detail and displayed based on product details through PHP. There are 2 files: default.php and view.html.php.
default.php :
<h1>Used <?php echo $this->CatName; ?> <?php echo $this->prodDet->prod_name;?> Toy for Sale </h1>
This correctly display the h1 tag. I want to generate meta title of the page and use this h1 output as generated in view.html.php. This line defines the title of the page :
$this->document->setTitle($title);
And this line defines header h1 :
"{$this->item->heading}";
Complete code :
protected function _prepareDocument()
{
$app = JFactory::getApplication();
$menus = $app->getMenu();
$title = null;
// Because the application sets a default page title,
// We need to get it from the menu item itself
$menu = $menus->getActive();
if ($menu)
{
$this->params->def('page_heading', $this->params->get('page_title', $menu->title));
}
else
{
$this->params->def('page_heading', JText::_('COM_USEDCAR_DEFAULT_PAGE_TITLE'));
}
$title = $this->params->get('page_title', '');
if (empty($title))
{
$title = $app->get('sitename');
}
elseif ($app->get('sitename_pagetitles', 0) == 1)
{
$title = JText::sprintf('JPAGETITLE', $app->get('sitename'), $title);
}
elseif ($app->get('sitename_pagetitles', 0) == 2)
{
$title = JText::sprintf('JPAGETITLE', $title, $app->get('sitename'));
}
$title = "{$this->item->heading}";
$this->document->setTitle($title);
if ($this->params->get('menu-meta_description'))
{
$this->document->setDescription($this->params->get('menu-meta_description'));
}
if ($this->params->get('menu-meta_keywords'))
{
$this->document->setMetadata('keywords', $this->params->get('menu-meta_keywords'));
}
if ($this->params->get('robots'))
{
$this->document->setMetadata('robots', $this->params->get('robots'));
}
}
Output in title tag is heading. How to put this h1 tag output instead of $title?
Here's what the title portion of your code does:
// getting title from params
$title = $this->params->get('page_title', '');
// trying to get it right
if (empty($title))
{
$title = $app->get('sitename');
}
elseif ($app->get('sitename_pagetitles', 0) == 1)
{
$title = JText::sprintf('JPAGETITLE', $app->get('sitename'), $title);
}
elseif ($app->get('sitename_pagetitles', 0) == 2)
{
$title = JText::sprintf('JPAGETITLE', $title, $app->get('sitename'));
}
// overwrite everything above with some value, making above code useless
$title = "{$this->item->heading}";
$this->document->setTitle($title);
I might be wrong but if I recall correctly, if a value doesn't exist it will return the variable name when cast into a string. Here "heading" might be empty.
You might want to change your code to something like this:
[...]
if(!title){
if(property_exists($this, 'item') && property_exists($this->item, 'heading') && $this->item->heading){
$title = $this->item->heading;
} else {
$title = sprintf('Used %s %s Toy for Sale' , $this->CatName, $this->prodDet->prod_name);
}
}
$this->document->setTitle($title);
You might as well like to save the title to session and reuse it everywhere:
[...]
$this->document->setTitle($title);
// save title to session
$_SESSION['page_title'] = $title;
and update the previous loop:
// getting title from params
$title = (isset($_SESSION['page_title']) && $_SESSION['page_title'])? $_SESSION['page_title'] : $this->params->get('page_title', '');
if (empty($title)){
[...]
Full code would be something like that:
[...]
session_id() || session_start();
$title = (isset($_SESSION['page_title']) && $_SESSION['page_title'])? $_SESSION['page_title'] : $this->params->get('page_title', '');
if(!title){
if(property_exists($this, 'item') && property_exists($this->item, 'heading') && $this->item->heading){
$title = $this->item->heading;
} else {
$title = sprintf('Used %s %s Toy for Sale' , $this->CatName, $this->prodDet->prod_name);
}
}
if (empty($title))
{
$title = $app->get('sitename');
}
elseif ($app->get('sitename_pagetitles', 0) == 1)
{
$title = JText::sprintf('JPAGETITLE', $app->get('sitename'), $title);
}
elseif ($app->get('sitename_pagetitles', 0) == 2)
{
$title = JText::sprintf('JPAGETITLE', $title, $app->get('sitename'));
}
$_SESSION['page_title'] = $title;
$this->document->setTitle($title);
[...]
You might as well just ditch everything and go like that if you'd like:
[...]
$title = $this->params->get('page_title', '');
if(!title){
if(property_exists($this, 'item') && property_exists($this->item, 'heading') && $this->item->heading) {
$title = $this->item->heading;
} elseif(
property_exists($this, 'CatName') &&
property_exists($this, 'prodDet') &&
property_exists($$this->prodDet, 'prod_name') &&
$this->CatName &&
$this->prodDet->prod_name
){
$title = sprintf('Used %s %s Toy for Sale' , $this->CatName, $this->prodDet->prod_name);
} else {
$title = $app->get('sitename');
}
}
$this->document->setTitle($title);
[...]
Code is untested but it should put you on the right track :)
Why don't you just send your h1 content to your php-document as GET parameter and then just output the it using echo inside the title tag? Unless you avoid dinamic echoing, this could be a fine solution for outputting text as title.
I would abstract away the logic of constructing the title/header to some function and then use this function to construct the title in both places.
function constructTitle($catName, $prodName) {
return "Used {$catName} {$prodName} Toy for Sale";
}
...
[in default.php]
<h1><?php echo constructTitle($this->CatName, $this->prodDet->prod_name); ?></h1>
[in view.html.php]
$this->document->setTitle(constructTitle(..., ...));
This allows you to have a single point to format your title while using it in several places.
The function needs to, obviously, be place in such position so that it can be accessed in both places and you need to have some way to get category name and product name in view.html.php. Im not familiar enough with joomla to know these things.
Edit:
To clarify, there is no real way to "extract" the title from the default.php as it is dynamic. You would need to process the php file then maybe you could do some regex magic, but this is in no way the proper solution to the problem.
you can just send your h1 content to your php-document as GET parameter and then output the it using echo in the title tag? Unless you avoid dynamic echoing,it would work.

How to add Buy Now button in cs-cart

I'm creating a buy-now button cs-cart add-on. I've created an add-on and I've made the add-to-cart functionality work. But cant redirect it to checkout page. I have used "fn_redirect("checkout")" function for the redirection. And used this function below this:
"fn_add_product_to_cart($_REQUEST['product_data'], $cart, $auth);"
What should I do for redirection?
Edit:
My controller (buy_now.php)
if ($mode == 'add') {
if (empty($auth['user_id']) && Registry::get('settings.General.allow_anonymous_shopping') != 'allow_shopping')
{
return array(CONTROLLER_STATUS_REDIRECT, "auth.login_form?return_url=" . urlencode($_REQUEST['return_url']));
}
// Add to cart button was pressed for single product on advanced list
if (!empty($dispatch_extra)) {
if (empty($_REQUEST['product_data'][$dispatch_extra]['amount'])) {
$_REQUEST['product_data'][$dispatch_extra]['amount'] = 1;
}
foreach ($_REQUEST['product_data'] as $key => $data) {
if ($key != $dispatch_extra && $key != 'custom_files') {
unset($_REQUEST['product_data'][$key]);
}
}
}
$prev_cart_products = empty($cart['products']) ? array() : $cart['products'];
fn_add_product_to_cart($_REQUEST['product_data'], $cart, $auth);
fn_save_cart_content($cart, $auth['user_id']);
$previous_state = md5(serialize($cart['products']));
$cart['change_cart_products'] = true;
fn_calculate_cart_content($cart, $auth, 'S', true, 'F', true);
if (md5(serialize($cart['products'])) != $previous_state && empty($cart['skip_notification'])) {
$product_cnt = 0;
$added_products = array();
if (!empty($added_products)) {
Registry::get('view')->assign('added_products', $added_products);
if (Registry::get('config.tweaks.disable_dhtml') && Registry::get('config.tweaks.redirect_to_cart')) {
Registry::get('view')->assign('continue_url', (!empty($_REQUEST['redirect_url']) && empty($_REQUEST['appearance']['details_page'])) ? $_REQUEST['redirect_url'] : $_SESSION['continue_url']);
}
fn_redirect(Registry::get('config.https_location') . "/checkout");
// $msg = Registry::get('view')->fetch('views/checkout/components/product_notification.tpl');
//fn_set_notification('I', __($product_cnt > 1 ? 'products_added_to_cart' : 'product_added_to_cart'), $msg, 'I');
$cart['recalculate'] = true;
} else {
fn_set_notification('N', __('notice'), __('product_in_cart'));
}
}
unset($cart['skip_notification']);
$_suffix = '.checkout';
}
`
hook template : add_to_cart.post.tpl
{$id = "buy_now_{$product.product_id}"}
<button id="opener_{$id}" name="dispatch[buy_now.add..{$product.product_id}]" class=" vs-button buynow_btn_">Buy Now</button>
I'd consider putting your fn_redirect("checkout") code into an if/else statement to see what's happening with it.
If you have this code:
fn_add_product_to_cart($_REQUEST['product_data'], $cart, $auth);
Then surely you can put the redirection to the checkout page underneath it?
$redirect_url = "http://testcheckout.com?test=test"; //change this!
$errorMessage = "I should have redirected already....";
if (!empty($redirect_url)) {
//if it's not empty then it should redirect.
fn_redirect($redirect_url);
//Then put a die statement in here just to check it's not executing
//anything else and you can see if the redirect has a problem:
die(print_r($errorMessage, true ));
}
else {
//otherwise there is a problem with the supplied redirect url (empty)
die(print_r($redirect_url, true ));
}
I'd also take a look at this page which might help more:
http://forum.cs-cart.com/topic/6873-direct-buy-button/
Hope that helps!

Don't have access to post meta in save_post action

So what I've noticed is that if I check for a meta value in my save_post function, it comes as blank, meaning the meta value hasn't actually been entered into the post. Looking at this code for example,
function order_mirror_create($post_id) {
global $post;
if($post->ID == ''){
$pid = $post_id;
} else {
$pid = $post->ID;
}
$videohost = get_post_meta($pid, 'video_provider', true);
if ($videohost == "UploadAnime") {
add_post_meta($pid, 'video_display_order', 1, true);
} else {
add_post_meta($pid, 'video_display_order', $videohost, true);
}
}
$videohost is actually blank, so get_post_meta($pid, 'video_provider', true) does not return the actual value of that meta field because it hasn't been created yet.
So in the code above, the else is always the one that runs, since $videohost is blank. How can I fix this? What action should I use that would run in such a manner that the post meta have already been added to the post.
You should change the following:
if ($videohost == "UploadAnime") {
add_post_meta($pid, 'video_display_order', 1, true);
} else {
add_post_meta($pid, 'video_display_order', $videohost, true);
}
To:
if ($videohost == "UploadAnime") {
update_post_meta($pid, 'video_display_order', 1, true);
} else {
update_post_meta($pid, 'video_display_order', $videohost, true);
}
I find add_post_meta has never really worked for me, whereas update_post_meta does.

Categories