Add target="_blank" to get_categories() WordPress code - php

I need to add target="_blank" to links I'm retrieving using this PHP code on a WordPress+WooCommerce installation:
$products = wc_get_products([
'status' => 'publish',
'limit' => -1,
'product_cat' => 'talleres'
]);
$locaciones = array_map(function ($product) {
return [
// more code goes here, I just deleted it for this question
'popup_html' => $product->get_categories() // this is where I need the target="_blank"
];
}, $products);
I tried with jQuery:
$(document).ready( function(){
$('a').attr('target', '_blank');
});
But it is not working, it doesn't add the target="_blank" to the link.
I'm thinking maybe this can be added directly to the PHP code?

You can use a php regex filter like preg_replace and search by "href" and replace the match putting the target="_blank" before the href resulting in "target='_blank' href"

You may use this script. you need to add class or parent div of Anchor tag otherwise it will add to all anchors.
jQuery(document).ready( function($){
$('a').each(function(){
$(this).attr('target', '_blank');
});
});

Since I needed to open all and every link on a new tab, I just used <base target="_blank"> for the entire page.

Related

Add new value at the end of URL Woo Elementor

I want to change following code from Elementor:
$this->add_render_attribute( 'button',
[
'rel' => 'nofollow',
'href' => $product->add_to_cart_url(),
'data-quantity' => ( isset( $settings['quantity'] ) ? $settings['quantity'] : 1 ),
'data-product_id' => $product->get_id(),
'class' => $class,
]
);
to add new value like this:
foreach (get_the_terms(get_the_ID(), 'producer') as $cat) {
echo $cat->name;
}
for the 'href' part in first code:
'href' => $product->add_to_cart_url(),
above 'href' code adds this value in path ?add-to-cart=2809 and I want to make it look like this ?add-to-cart=2809&wdm_name=MY-CODE-FOR-EACH-ABOVE-CREATE-VALUE-HERE
How do I implement second code inside HREF part so I dont get the error.
Thanks
Actually, just a few days ago I had the need of changing existing URLs inside href for each category on a widget my shop page.
So for my case I ended up creating this code:
var $ = jQuery;
$(document).ready(function(){
var search = window.location.search; // Get all existing search params in URL
var urlOrigin = document.location.origin; // Get current URL (only domain)
//console.log(urlOrigin); // For testing in browser console
$('.widget-area .product-categories li a').each(function(){ // You can mobify this to get your elements where you have them listed
var catSlug = $(this).attr('href').replace(urlOrigin,''); // I am doing this because I need to get the slug for each category to then append to the final URL
url = urlOrigin+catSlug+search; // Final URL mounted, here is where you can put your variable to set the code you want for each URL
$(this).attr('href',url); // Associate this new generated URL to the element
});
});
Since it is not clear where/how you want to use it, I leave my code here in case you can use it to adapt to your case, which I belive you can.

Having a issue regarding DataTables paggingType = "input"

i'm using datatables and for pagination i've used paggingtype = "input" and also included its plugin cdn but unfortunately when i used this, it is automatically disabling the First Next Previous and Last pagination buttons and i needed them enabled so that i can use them alongwith input text field.. Any help will be a huge favor
Here's my JavaScript:
$(document).ready(function () {
$('#categories').DataTable({
"processing" : true,
"pagingType" : "input",
"ajax" : {
"url" : ajaxurl + "/ManageCategories/fetchCategories",
"type" : "POST"
}
});
});
and here's my DataTable showing output:
thanks,
Ameer
I got a solution to my question i.e. whenever "paggingType" is used it will overwrite the default "paggingType" so in order to get a text input i can add it manually by inserting this javaScript:
$("div.toolbar").html('<div><input type="text" id="pageJump" placeholder="page">
<button id="jump" type="button">Go</button></div>');
$('#jump').click( function() {
table.page(parseInt($('#pageJump').val())-1).draw(false);
})
try simple_numbers and check
"pagingType": "simple_numbers"
simple_numbers: function ( page, pages ) {
return [ 'previous', _numbers(page, pages), 'next' ];
},
simple_numbers - 'Previous' and 'Next' buttons, plus page numbers
pagingType Datatables

How to run CookieList (MODx extension) with ajax

Please, explain to me how to run snippet CookieList with ajax?
I tried next:
1. Created snippet ajaxCookieList:
<?php
if (isset($_POST["action"])) {
$values = $modx->runSnippet('addToCookieLIst',array(
'value' => $_POST['action']
));
$output = $modx->runSnippet('pdoResources',[
'parents' => 6,
'resources' => $values,
'tpl' => 'popup.favorites.item',
'includeTVs' => 'header.bgImage,franchise.logo,franchise.price,title,subtitle',
'prepareTVs' => '1',
'hideContainers' => '1'
]);
return $output;
}
Then i created chunk with this code:
<script>
jQuery(function($){
$('a.franchise-pin, a.franchise-favorite-add').click(function(e){
var value = $(this).data('value');
$.post(document.location.href, {action: value}, function(data) {
$('#favorites').html(data);
$('#favorites').modal('show');
});
e.preventDefault();
});
});
</script>
But response is all page..
What is wrong?
What I usually do in such cases:
I place a snippet call ([[!ajaxCookieList]]) on a service page
accessible via URL like /page-with-snippet/
In JS (ajax) I use that URL to which I send parameters
The snippet has to get the parameters I send. So, I really call it like this: [[!ajaxCookieList? &action=[[!#POST.action]]]]
Access to parameters in snippets is possible like this: $option = $modx->getOption('action', $scriptProperties, 'default_value', true);
I do my stuff in the snippet
But in your case, I think, everything can be simpler. You use one of the pdoTools snippets and if I am not mistaken, you can just place pdoResources snippet call on a page (/page-with-snippet/) like this:
[[pdoResources?
&parents=`6`
&resources=`[[!addToCookieLIst? &value=`[[!#POST.action]]` ]]` // your snippet should return comma-separated list of resources` ids that you pass then to pdoResources
&tpl=`popup.favorites.item`
&includeTVs=`header.bgImage,franchise.logo,franchise.price,title,subtitle`
&prepareTVs=`1`
&hideContainers=`1`
]]
and now you can send parameters to this page (/page-with-snippet/) via AJAX and get the results if there are any. I hope I didn't mess anything - you had better check it again, but you get the idea at least :) BTW, check this article on modx.com that teaches how to write a good snippet.
Also another minor issue: as it has been pointed out here, the use of window.location is preferable to document.location.
Here is another solution I did. I used pdoResources. Hope that you will understand my code and customize it for yourselves.
Create snippet ajaxCookieList
Paste JS-code in your custom.js file
Simple markup for resources. Insert it in the chunk:
Add to wish list
Remove from wish list
Thas all:)

How to add non HTML attribute in Yii's chtml::image htmloptions

I am trying to add non HTML attribute in htmlOptions array of Yii's CHTML::image($url, $alt, $htmlOptions), but in rendered page these attributes does not get added.
Basically i want to use lazy loading of images which needs to store original image url in 'data-origional' and a placeholder in SRC tag. attribute. For some reasons i can't use a direct HTML <img /> tag in my code.
Thanks for any suggestions guys.
This code works fine:
echo CHtml::image('http://google.com/images/srpr/logo3w.png', '', array(
'data-original' => 'original',
'another-attribute' => 'bla-bla-bla',
));
It returns:
<img
alt="" src="http://google.com/images/srpr/logo3w.png"
another-attribute="bla-bla-bla"
data-original="original"
>

$ajax->link() doesn't work in Firefox?

I am reading Beginning CakePHP, and to make it so that you can vote on comments, it tells you to create a couple of AJAX links:
<?=$ajax->link('<li>up</li>',
'/comments/vote/up/'.$comment['Comment']['id'],
array('update' => 'vote_'.$comment['Comment']['id']),
null, false);?>
<?=$ajax->link('<li>down</li>',
'/comments/vote/down/'.$comment['Comment']['id'],
array('update' => 'vote_'.$comment['Comment']['id']),
null, false);?>
This works fine in IE, but in FF it doesn't do anything at all. It doesn't even reach the controller or model, because the links it generates don't do anything.
The HTML it generates looks like this:
<a id="link2128392960" onclick=" event.returnValue = false; return false;" href="/blog/comments/vote/up/1"/>
<li>
<a id="link2128392960" onclick=" event.returnValue = false; return false;" href="/blog/comments/vote/up/1">up</a>
</li>
<script type="text/javascript">
//<![CDATA[
Event.observe('link2128392960', 'click', function(event) { new Ajax.Updater('vote-1','/blog/comments/vote/up/1', {asynchronous:true, evalScripts:true, requestHeaders:['X-Update', 'vote-1']}) }, false);
//]]>
</script>
Firstly, the HTML output you pasted isn't accurate. It looks like you copied this from Firebug or something instead of your browsers actual 'View Source' page. CakePHP doesn't actually generate self-closing anchor tags in this scenario (<a /> as opposed to <a></a>) like your example shows.
I believe this points out your problem though. The fact that your browser's HTML parser (or Firebug's) tried to correct the code at runtime points to your HTML being malformed. Specifically, you cannot put a block-level element (<li>) inside an inline element (<a>).
<div class="actions">
<ul>
<li>
<?php echo $ajax->link('up', array(
'controller' => 'comments',
'action' => 'vote',
'up',
$comment['Comment']['id']
), array(
'update' => 'vote_' . $comment['Comment']['id']
), null, false); ?>
</li>
<li>
<?php echo $ajax->link('down', array(
'controller' => 'comments',
'action' => 'vote',
'down',
$comment['Comment']['id']
), array(
'update' => 'vote_' . $comment['Comment']['id']
), null, false); ?>
</li>
</ul>
</div>
The above example will produce valid HTML as the inline <a> elements will then be wholly nested within block-level <li> elements. This should hopefully fix the functionality of your page in standards-compliant browsers.
I don't think it's valid HTML to have an anchor tag without an ending anchor tag, like in your first link:
<a id="link2128392960" onclick=" event.returnValue = false; return false;" href="/blog/comments/vote/up/1"/>
Maybe Firefox is mad at you for that.
You also have two tags with the same id, which is also invalid HTML, but might also mean that your Javascript is searching for clicks on the second link but that link is being "eaten" by the first link because you never ended it.
Just throwing out ideas ;p
Using php short tags ("<?") can be considered a bad practice, plus they are being removed in php 6.

Categories