I have a WordPress theme that outputs a portfolio grid based on a shortcode. If you click on the portfolio featured image, you open a modal box with the project details.
I am trying to use google's virtual pageview tracking to log a pageview when a user clicks to open the modal box.
_gaq.push(['_trackPageview', '/downloads/pdfs/corporateBrief.pdf']);
https://developers.google.com/analytics/devguides/collection/gajs/asyncMigrationExamples
Here is how the the portfolio html is generated in my portfolio.php file:
// GENERATE HTML
$html .= '<li class="item' . $thumb_classes . '" data-title="' . get_the_title() . '">';
$html .= '<a href="' . get_the_permalink() . '"';
$html .= $enable_modal ? ' data-ajax-selector=".portfolio-detail"' : '';
$html .= ' class="item-image';
$html .= $enable_modal ? ' m-open-ajax-modal-project' : '';
$html .= '">' . $thumb_image . '</a>';
$html .= $enable_title || $enable_excerpt ? '<div class="item-info">' : '';
$html .= $enable_title ? '<h4 class="m-secondary-font"><a href="' . get_the_permalink() . '"' : '';
$html .= $enable_title && $enable_modal ? ' data-ajax-selector=".portfolio-detail" class="m-open-ajax-modal-project"' : '';
$html .= $enable_title ? '>' . get_the_title() . '</a></h4>' : '';
$html .= $enable_excerpt ? wpautop( get_the_excerpt() ) : '';
$html .= $enable_title || $enable_excerpt ? '</div>' : '';
$html .= $enable_permalink ? '<div class="item-tools"><i class="fa fa-link"></i></div>' : '';
$html .= '</li>';
I've tried adding the code below below the second line but it is causing an error:
$html .= ' onClick="_gaq.push(['_trackPageview', '/downloads/pdfs/corporateBrief.pdf']);"';
Parse error: syntax error, unexpected '_trackPageview' (T_STRING) in
/home/abc/public_html/wp-content/plugins/toolkit/shortcode-generator/shortcodes/portfolio.php
on line 234
I am assuming I should be formatting that onClick code differently but don't know where to start. From what I can tell, php doesn't like the commas ' necessary in the javascript code. Perhaps I'm way off base as well.
Any help would be appreciated!
you forgot to escape the quote mark. so php got error
$html .= ' onClick="_gaq.push([\'_trackPageview\', \'/downloads/pdfs/corporateBrief.pdf\']);"';
would work
And i think using this kind of syntax is better than using dot to contact strings.
<?php
$google = 'http://www.google.com';
?>
<li>
google
<!-- works for php5.3+ -->
goggle2
</li>
Also you may try some editor that support php code highlight, they will show you the problem
Related
Is it possible to have a php array sorted by clicking on a button?(Alphabetically or by year)
I am using fullpage.js and Columnizer jQuery plugin. Unfortunately, the problem is that I have to make a new table for each new slide. (Maybe someone knows a better solution?)
Or is it more useful javascript / jquery to use?
here is the Code:
// get table data from Plugin TablePress
$table = TablePress::$model_table->load( $atts['table-id'], true, true );
//only get the important data
$data = $table['data'];
$output = '<div id="tablecontest-slider">';
$output .= '<div class="section" id="section0">';
array_shift($data);
$counter = 1;
foreach ($data as $value) {
if ($counter == 1) {
$output .= '<div class="slide">';
$output .= '<div class="columnize">';
$output .= '<table>';
}
$output .= '<tr>';
$output .= '<td>' . $value[0] . '</td>';
$output .= '<td>' . $value[1] . '</td>';
$output .= '<td>' . $value[2] . '</td>';
$output .= '</tr>';
$counter++;
if ($counter == 21) {
$counter = 1;
$output .= '</table>';
$output .= '</div>';
$output .= '</div>';
}
}
$output .= '</div>';
$output .= '</div>';
return $output;
I dont know how to sort the array by click on a button..
Thank you for ideas and suggestions
No, you can't. PHP is running on your server and delivers the page. So there is no PHP-Array anymore once you render the site in your browser.
If you would get your data via an AJAX-call you COULD implement sorting and filtering on the server.
You can't directly sort anything on PHP from JS. BUT you can send an Ajax query on the button click, and respond with the PHP array sorted.
I need to output some strings in the onclick anchor function.I have used proper escaping but anyhow its returning error.
My code is:
$output .= '<a title="Minus this" href="#" onclick = removefromCart("' . $item . '", "' . $nonce . '", ' . $deductQty . ');></a>';
And also used this :
$output .= '<a title="Minus this" href="#" onclick = "removefromCart("' . $item . '", "' . $nonce . '", ' . $deductQty . ')"></a>';
But in both cases there is Uncaught SyntaxError: Unexpected token }
The quotes is totally wrong. Do this way, using the first one:
$output .= '<a title="Minus this" href="#" onclick=\'removefromCart("' . $item . '", "' . $nonce . '", ' . $deductQty . ');\'></a>';
See the 's I have added and removed spaces? And please, next time, don't mingle both PHP and JavaScript. It's confusingly dangerous.
See also:
Are single quotes allowed in HTML?
Is there a better way to write this php code?
You should use single quotations to represent string in HTML element
$output .= '<a title="Minus this" href="#" onclick = "removefromCart(\'' . $item . '\', \'' . $nonce . '\', ' . $deductQty . ')"></a>';
For greater than or equal to PHP5
$output .= '<a title="Minus this" href="#" onclick = removefromCart($item,$nonce,$deductQty);></a>';
For Less than PHP5 try it
$output .= '<a title="Minus this" href="#" onclick = removefromCart(' . $item . ',' . $nonce . ', ' . $deductQty . ');></a>';
I have an issue with my grid, after loading data and enabling vertical scroll, i find my grid loaded with additional space after the my rows. I want that removed, any idea why i am getting this ?
This is my grid with the scroll bar
This is the problem
I just want the scroll bar to stop at number 20, i don't want all that additional space.
And this is my grid setting :
$html .= '$("#'. $this->_name .'").jqGrid({';
$html .= 'url:"tools/grid_server_calls.php",';
$html .= 'datatype: "json",';
$html .= 'autoencode: true,';
$html .= 'mtype: "POST",';
$html .= 'postData:{inputs:'. $this->_server_inputs .'},';
$html .= 'gridview: true,';
$html .= 'ignoreCase: true,';
//$html .= 'loadui: "block",';
$html .= 'hidegrid: false,';
$html .= 'width: "' . $this->_width_grid . 'px",';
$html .= 'forceFit: true,';
$html .= 'pager: "#pager_'.$this->_name.'",';
$html .= 'rowNum: ' . $this->_rowNum . ',';
$html .= 'rowList: [' . $this->_rowList . '],';
if( $this->_scroll_enabled ){
$html .= 'scroll: true,';
$html .= 'height: "230px",';
}
else{
$html .= 'height: "auto",';
}
$html .= 'viewrecords: true,';
$html .= 'emptyrecords:"'.$this->_message_no_records.'",';
$html .= 'recordtext:"{0} - {1} / {2}",';
$html .= 'altRows: true,';
$html .= 'multiselect: "' . $this->_multiselect .'",';
$html .= 'altclass: "color_line_grid",';
$html .= 'caption: "' . $this->_caption . '",';
$html .= 'colNames: [' . $this->_colNames . '],';
$html .= 'colModel: [' . $this->_colModel . '],';
$html .= 'sortname: "' . $this->_initialSort . '",';
$html .= 'sortorder: "asc",';
if(!$this->_search_enabled){
$html .= 'pgbuttons: false,';
$html .= 'pgtext: null,';
$html .= 'rowList: [],';
$html .= 'cmTemplate: {sortable:false},';
}
I got to the root of the problem, it was as simple as learning to read before asking dumb questions, in fact you need not set scroll to true, you just need to set a height, when the data exceeds the height that was set, the scroll bar will appear automatically 'In my case' .
I've been trying to add Javascript:void(0)to my magento website's navigation bar for a long time.
I read many articles about this subject and almost all of them says I should edit
/app/code/core/Mage/Catalog/Block/Navigation.php or copy this to local folder end edit there.
here one of the popular way that i followed;
To remove url, href functionality of the top menu categories, you can take the following steps:
Create some folder as this path: app/code/local/Mage/Catalog/Block
Copy file Navigation.php from app/code/core/Mage/Catalog/Block to
app/code/local/Mage/Catalog/Block
Go to function _renderCategoryMenuItemHtml()
Replace this code
$html[] = '<a href="'.$this->getCategoryUrl($category).'"'.$linkClass.'>';
$html[] = '<span>' . $this->escapeHtml($category->getName()) . '</span>';
$html[] = '</a>';
with this code
if($category->getLevel()== 2 && $hasActiveChildren) {
$html[] = '<a href="[removed]void(0);"'.$linkClass.'>';
$html[] = '<span>' . $this->escapeHtml($category->getName()) . '</span>';
$html[] = '</a>';
} else {
$html[] = '<a href="'.$this->getCategoryUrl($category).'"'.$linkClass.'>';
$html[] = '<span>' . $this->escapeHtml($category->getName()) . '</span>';
$html[] = '</a>';
}
But it didn't work for me. Weirdly even if i delete /app/code/core/Mage/Catalog/Block/Navigation.php the navigation menu works just fine. Were it gets the code for working I have no idea.
Maybe you have an idea that can help me. I just want to put Javascript:void(0) to the navigation menu.
I feel it's issue of cache or Magento Compiler. I suggest you to do the following steps from magento admin.
go to admin->System->Cache Management and clear/flush all the cache
go to admin->System->Tools->Compilation. Check if compilation is enabled then disabled it and clear cache again and run your compilation process
I was also looking to solve this, I think the problem is that the code you linked only works on 1.7 and not on 1.9?
The solution i came up with
copy Topmenu.php from app\code\core\Mage\Page\Block\Html to app\code\local\Mage\Page\Block\Html replace on line 131:
$html .= '<li ' . $this->_getRenderedMenuItemAttributes($child) . '>';
$html .= '<a href="' . $child->getUrl() . '" ' . $outermostClassCode . '><span>'
. $this->escapeHtml($child->getName()) . '</span></a>';
to
if ($child->hasChildren()) {
$html .= '<li ' . $this->_getRenderedMenuItemAttributes($child) . '>';
$html .= '<a href=\'javascript:void(0);\'><span>'
. $this->escapeHtml($child->getName()) . '</span></a>';
}
else{
$html .= '<li ' . $this->_getRenderedMenuItemAttributes($child) . '>';
$html .= '<a href="' . $child->getUrl() . '" ' . $outermostClassCode . '><span>'
. $this->escapeHtml($child->getName()) . '</span></a>';
}
Hope this helps.
I'm generating a custom field on the Wordpress admin panel. I want to add 2 buttons:
1. Add another
2. Remove field
The add button should generate the same field. I need help coding the javascript that generates the fields.
Here is my code:
$html = '';
$html .= '<div class="pyre_metabox_field">';
$html .= '<label for="pyre_' . $id . '">';
$html .= $label;
$html .= '</label>';
$html .= '<div class="field">';
$html .= '<input type="text" id="pyre_' . $id . '" name="pyre_' . $id . '" value="' . get_post_meta($post->ID, 'pyre_' . $id, true) . '" />';
if($desc) {
$html .= '<p>' . $desc . '</p>';
}
$html .= '<p style="margin-bottom:15px; padding-top:5px;clear:both;">Add Remove</p>';
$html .= '</div>';
$html .= '</div>';
Have you tried : Advanced Custom Fields ?
This plugin is really simple to use, and I'm pretty sure it can do what you're trying to do. It may save you a lot of work :)