How enable .html in wordpress pagination - pagination

I have to use wordpress with .html extension, and i am using plugin to make .html extension at end of URL, that is working fine, but my pagination becoming failed, it makes URL as /page-url.html/page/2 that goes on Page not found, how can I enable .html in pagination ?
Thanks in advance.
By the way, i did some work like
add_filter('get_pagenum_link', function($url) {
$html = strrpos( $url, '.html' );
if( $html > 0 ){
$base_link = substr( $url, 0, strrpos( $url, '/page' ) );
$page_number = basename($url);
$new_url = $base_link.'/?iter='.$page_number;
return $new_url;
}
else{
return $url;
}
});
but how can i tell wordpress the page link of pagination should follow this ? i mean active and inactive links of pagination

I have done this task by adding filter for URL of pagination, and it works fine with .html extension. Following is code to run pagination with .html extension.
add_filter('get_pagenum_link', function($url) {
$current_url_arr = explode("?", $_SERVER['REQUEST_URI']);
$current_url = $current_url_arr[0];
$html = strrpos( $url, '.html' );
$page_number = 1;
//$page_number = $_GET['iter'] + 1;
if( $html > 0 ){
$basename = basename($url);
$page_number = substr( $basename, 0, strrpos( $basename , '?') );
$new_url = $current_url.'/?iter='.$page_number;
return $new_url;
}
else{
return $url;
}
});
and on above the query parameter, write the following code
if( $_GET['iter'] > 0 ){
$paged = $_GET['iter'];
}
Enjoy!!!

Related

Trying to create a shortcode to insert a react app

I need to integrate a react application inside a wordpress website. I created a custom plugin and a shortcode but the shortcode renders an empty div. It's my first wordpress plugin so I might be missing something.
My plugin directory looks like this:
/public_html/wp-content/plugins/PatSearch
- PatSearch.php
/public_html/wp-content/plugins/PatSearch/includes
- enqueue.php
- shortcode.php
/public_html/wp-content/plugins/PatSearch/widget
- Contains the root of the react application. It has been build and works when I visit the build directory
PatSearch.php
<?php
/**
* #wordpress-plugin
* Plugin Name: Ajout d'un app React pour la recherche
*/
defined( 'ABSPATH' ) or die( 'Direct script access diallowed.' );
define( 'PAT_WIDGET_PATH', plugin_dir_path( __FILE__ ) . '/widget' );
define( 'PAT_ASSET_MANIFEST', PAT_WIDGET_PATH . '/build/asset-manifest.json' );
define( 'PAT_INCLUDES', plugin_dir_path( __FILE__ ) . '/includes' );
require_once( PAT_INCLUDES . '/enqueue.php' );
require_once( PAT_INCLUDES . '/shortcode.php' );
enqueue.php
<?php
// This file enqueues scripts and styles
defined( 'ABSPATH' ) or die( 'Direct script access disallowed.' );
add_action( 'init', function() {
add_filter( 'script_loader_tag', function( $tag, $handle ) {
if ( ! preg_match( '/^pat-/', $handle ) ) { return $tag; }
return str_replace( ' src', ' async defer src', $tag );
}, 10, 2 );
add_action( 'wp_enqueue_scripts', function() {
$asset_manifest = json_decode( file_get_contents( PAT_ASSET_MANIFEST ), true )['files'];
if ( isset( $asset_manifest[ 'main.css' ] ) ) {
wp_enqueue_style( 'pat', get_site_url() . $asset_manifest[ 'main.css' ] );
}
wp_enqueue_script( 'pat-runtime', get_site_url() . $asset_manifest[ 'runtime~main.js' ], array(), null, true );
wp_enqueue_script( 'pat-main', get_site_url() . $asset_manifest[ 'main.js' ], array('pat-runtime'), null, true );
foreach ( $asset_manifest as $key => $value ) {
if ( preg_match( '#static/js/(.*)\.chunk\.js#', $key, $matches ) ) {
if ( $matches && is_array( $matches ) && count( $matches ) === 2 ) {
$name = "pat-" . preg_replace( '/[^A-Za-z0-9_]/', '-', $matches[1] );
wp_enqueue_script( $name, get_site_url() . $value, array( 'pat-main' ), null, true );
}
}
if ( preg_match( '#static/css/(.*)\.chunk\.css#', $key, $matches ) ) {
if ( $matches && is_array( $matches ) && count( $matches ) == 2 ) {
$name = "pat-" . preg_replace( '/[^A-Za-z0-9_]/', '-', $matches[1] );
wp_enqueue_style( $name, get_site_url() . $value, array( 'pat' ), null );
}
}
}
});
});
shortcode.php
<?php
// This file enqueues a shortcode.
defined( 'ABSPATH' ) or die( 'Direct script access disallowed.' );
add_shortcode( 'pat_widget', function( $atts ) {
$default_atts = array();
$args = shortcode_atts( $default_atts, $atts );
return "<div id='pat-root'></div>";
});
I created a page and added the shortcode [pat_widget] saved it and visited the page but the react application located at /wp-content/plugins/PatSearch/widget/build/ was not embed like I thought ... When I look at the source code of the page, I can only see an empty <div id='pat-root'></div>
I found what the problem was. The wordpress side of things was working fine ... I had to modify the React index.js file that was generated in the build.
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
// Commented this line
// ReactDOM.render(<App />, document.getElementById("root"));
// Added this to match my shortcode requirements
const target = document.getElementById('pat-root');
if (target) { ReactDOM.render(<App />, target); }

dompdf: base64 encoded SVGs aren't rendered correctly

I'm rendering a PDF in Symfony with dompdf.
allow_url_fopen is deactivated on my server. So I insert every image as base64-encoded.
$imagesPath = "/var/www/project/web/print/img/";
$images = array(
"###FRONTPAGE###" => $imagesPath . "company_frontpage_2016.svg",
"###LOGO###" => $imagesPath . "company_logo.svg"
);
foreach( $images as $image => $imageFile ) {
if ( is_readable( $imageFile ) ) {
$base64String = base64_encode( fread( fopen( $imageFile, "r" ), filesize( $imageFile ) ) );
$html = str_replace( $image, "data:image/svg+xml;base64," . $base64String, $html );
} else {
$html = str_replace( $image, "File nicht lesbar", $html );
}
}
The absolute server path didn't work for me. Does anyone know a better solution for this?
I'm happy that I see the SVG in the PDF now. But they aren't rendered correctly. Any idea why?

Expand/Collapse Works for First Two Web Parts Only

I am trying to Maximize/Minimize a Document list Web Part.
I used the following code from http://blog.pathtosharepoint.com/2008/10/25/expandcollapse-buttons-for-your-web-parts/
and it works for my first two Web Parts but not my other two. All I did was include their titles within my coding as a change. Therefore, there shouldn't be any issue with the display.
Any suggestions on what can cause this?
The only changes I made after the first 2 were working was I added 2 more title1 to include the other 2 Web Parts.
As you can see, the bottom 2 don't fully minimize on page load
fYI: inside the coding.. the titles are changed to "... Orders" but the ... is in place of the actual name
<script type="text/javascript">
// Expand/Collapse Buttons
function WPToggle(thisId, ImageId)
{
if (document.getElementById(thisId).style.display=="none")
{
document.getElementById(thisId).style.display="";
document.getElementById(ImageId).src = "/_layouts/images/minus.gif";
}
else
{
document.getElementById(thisId).style.display="none";
document.getElementById(ImageId).src = "/_layouts/images/plus.gif";
}
}
function ExpandCollapseBody()
{
var i = 1;
var WPid = "WebPartWPQ1" ;
var WPtitleid = "WebPartTitleWPQ1" ;
var Toggleid = "ToggleImage1" ;
do
{
try
{
title1 = document.getElementById(WPtitleid).getAttribute("title");
if (title1 == "... Orders" || title1 == "... Orders" || title1 =="... Orders" || title1 == "... Orders")
{
document.getElementById(WPtitleid).innerHTML = '<IMG id="' + Toggleid + '" onClick="WPToggle(\'' + WPid + '\',\'' + Toggleid +
'\')" alt="Expand/Collapse" style="margin:6px 5px 0px 2px; float:left; cursor:pointer;" src="/_layouts/images/minus.gif" />' +
document.getElementById(WPtitleid).innerHTML ;
document.getElementById(Toggleid).src = "/_layouts/images/plus.gif";
}
}
catch(err) {}
i = i + 1;
WPid = "WebPartWPQ" + i ;
WPtitleid = "WebPartTitleWPQ" + i;
Toggleid = "ToggleImage" + i;
} while (document.getElementById(WPid))
}
_spBodyOnLoadFunctionNames.push("ExpandCollapseBody()");
</script>
I guess I see that by default, the more I add more webparts they start at expanded rather than collapse....
I see. I need the following condition of Chrome: minimize for the Web Part

Wordpress filter to handle custom action button on custom post type

I have created custom post type called campaign, then I have create custom action button using post_row_actions filter like this.
function hipwee_add_action_button($actions, $post){
if(get_post_type() === 'campaign'){
$actions['export'] = 'Export Result';
}
return $actions;
}
add_filter( 'post_row_actions', 'hipwee_add_action_button', 10, 2 );
as you can see above, I added new action button called "Export Result",
but now, how to add a function to handle the export, is there available wordpress filter to put my custom action handler?
Try this code:
function hipwee_add_action_button($actions, $post){
if(get_post_type() === 'campaign'){
$url = add_query_arg(
array(
'post_id' => $post->ID,
'my_action' => 'custom_export_post',
)
);
$actions['export'] = '<a href="' . esc_url( $url ) . '" target="_blank" >Export Result</a>';
}
return $actions;
}
add_filter( 'post_row_actions', 'hipwee_add_action_button', 10, 2 );
add_action( 'admin_init', 'custom_export_function' );
function custom_export_function(){
if ( isset( $_REQUEST['my_action'] ) &&
'custom_export_post' == $_REQUEST['my_action'] ) {
$data = array(
'hello' => 'world'
);
header('Content-Type: application/json');
header('Content-Disposition: attachment; filename="sample.json"');
echo json_encode($data);
exit;
}
}
What it does:
When Export Result button is clicked, it opens new window with parameter my_action with value custom_export_post. Now another function custom_export_function is hooked to admin_init. This function acts as the main exporter. In the example a sample array is exported to JSON file. You can now customize function as you require.

Opencart 1.5.4 Search by Description

I have read various entries about searching by description and subcategories in opencart by default but I have a unique problem. I have two header files because my site has 2 headers... one for the home page and one for every other page.
Home Page:
https://garrysun.com/
Other Page:
https://garrysun.com/ayurveda-products/categories
When I search on the home page I get the correct results (search the word "heart") but when I search any other page it doesn't return the search for descriptions or subcategories.
Home Page Search Results:https://garrysun.com/index.php?route=product/search&filter_description=true&filter_sub_category=true&filter_name=heart
Other Page Search Results:https://garrysun.com/index.php?route=product/search&filter_name=heart
As you can see, when I search the other page the extra code is not being added to search in descriptions and subcategories.
So why is this new code that I added working for the home page an not any other page?
To make this search function work I have changed the common.js file to look like this (adding the two lines below each "url= $(base..." section:
/* Search */
$('.button-search').bind('click', function() {
url = $('base').attr('href') + 'index.php?route=product/search';
url += '&filter_description=true'; // ADDED this to search descriptions
url += '&filter_sub_category=true'; // ADDED this to search sub-categories
var filter_name = $('input[name=\'filter_name\']').attr('value');
if (filter_name) {
url += '&filter_name=' + encodeURIComponent(filter_name) ;
}
location = url;
});
$('#header input[name=\'filter_name\']').bind('keydown', function(e) {
if (e.keyCode == 13) {
url = $('base').attr('href') + 'index.php?route=product/search';
url += '&filter_description=true'; // ADDED this to search descriptions
url += '&filter_sub_category=true'; // ADDED this to search sub-categories
var filter_name = $('input[name=\'filter_name\']').attr('value');
if (filter_name) {
url += '&filter_name=' + encodeURIComponent(filter_name) ;
}
location = url;
}
});
Both header files use the same code to call the search function:
<div id="search">
<div class="button-search"></div>
<?php if ($filter_name) { ?>
<input type="text" name="filter_name" value="<?php echo $filter_name; ?>" />
<?php } else { ?>
<input type="text" name="filter_name" value="<?php echo $text_search; ?>" onclick="this.value = '';" onkeydown="this.style.color = '#000000';" />
<?php } ?>
</div>
</div>
After trying to figure out what's wrong in your code for few minutes (unsuccessfully), I ran a network debugging and found out that nothing is wrong with your code, you are just calling 2 different Javascript files(!):
On your home page, you are using common.js that is located at https://garrysun.com/catalog/view/javascript/common.js.
On your category pages, you are using common.js that is located at https://garrysun.com/catalog/view/javascript/add2cart-go2cart/common.js.
The 2nd one does not include your modifications, and looks like this:
$('.button-search').bind('click', function() {
url = $('base').attr('href') + 'index.php?route=product/search';
var filter_name = $('input[name=\'filter_name\']').attr('value');
if (filter_name) {
url += '&filter_name=' + encodeURIComponent(filter_name);
}
location = url;
});
$('#header input[name=\'filter_name\']').bind('keydown', function(e) {
if (e.keyCode == 13) {
url = $('base').attr('href') + 'index.php?route=product/search';
var filter_name = $('input[name=\'filter_name\']').attr('value');
if (filter_name) {
url += '&filter_name=' + encodeURIComponent(filter_name);
}
location = url;
}
});
Vuala.
Hope this helps!

Resources