Wordpress filter to handle custom action button on custom post type - 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.

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); }

Pagination in search.php not working correctly (Wordpress)

In the past few days I was trying to create a search.php template for results that contain my CPT. As long as the search works itself, pagination not. Every link in pagination redirect to 404. I was trying a lot of possibility solutions from Stack, FB and others but with no results. Instead of showing to you my code wich is obviously not working correctly, I thought how great woultd be when someone could paste here a tested by himself a simple search.php template wchich contain CPT in results and with working pagination? Then I could compare with my code and find a solution for this annoying popular issue :)
Thanks!
edit:
If somoeone is curious, this is my current search.php:
<div class="center">
<?php
// Define custom query parameters
$custom_query_args = array(
'post_type' => array('drzewa_formowane', 'pre_bonsai'),
'post_status' => 'publish',
'order' => 'asc',
's' => $s,
'paged' => $paged
);
// Get current page and append to custom query parameters array
$custom_query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
// Instantiate custom query
$custom_query = new WP_Query( $custom_query_args );
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $custom_query;
// Output custom query loop
if ( $custom_query->have_posts() ) :
while ( $custom_query->have_posts() ) :
$custom_query->the_post(); ?>
<?php include 'product.php' ?>
<? endwhile;
endif;
// Reset postdata
wp_reset_postdata(); ?>
<nav class="pagination">
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_next' => true,
'prev_text' => __(' « '),
'next_text' => __(' » '),
'type' => 'plain',
) ); ?>
</nav>
<?php
// Reset main query object
$wp_query = NULL;
$wp_query = $temp_query;
?>
</div>
I was also trying with simple
if ( have_posts() ) : while ( have_posts() ) : the_post();
without WP_Query and I was trying include CPT to search results via "pre_get_posts" but then was no results from my CPT...

How to display attached images on single.php file in wordpress

Maybe someone solved this problem sorry if I am asking again this question. I have one post displaying on front page now this post has one feature Image along with other images and gallery.
What I want when user click on post so it will go on single.php to show full post view. I want to display on this single.php view an image and some text as well as a custom gallery images but not the feature image.
how can I achieve this I wrote one code but it did not work.
function displayPostImages() {
global $post; function
$attachments = get_children(array('post_parent' => $post->ID, 'order' => 'ASC', 'orderby' => 'menu_order', 'post_type' => 'attachment', 'post_mime_type' => 'image' ));
$image = get_post_thumbnail_id( $post->ID); // featured_image ID
$featured_image = wp_get_attachment_image_src($image);
if ($attachments) { // if there are images attached to posting, start the flexslider markup
foreach ( $attachments as $attachment_id => $attachment ) {
if($attachment->ID != $image && $featured_image ){
$img_source = wp_get_attachment_image_src( $attachment_id, 'full' )[0];
//print_r($img_source);
?>
// here will be HTML to display TOP images text and gallery images.
<?php
}
}
}
}
displayPostImages();
?>
and what is returned your function?
I don`t see "return" or "echo $someresult";
empty or not this array $attachments = get_children....

How enable .html in wordpress 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!!!

drupal 6 -----why theme() can't output

the code i put in the mytheme template.php
function mytheme_theme(){
return array(
'mytheme_example' => 'example',
'argument' => array('myvar' => null),
);
}
the code i put in the node.tpl.php
<?php
$html = "";
$myvar = "hello,world";
$html .= theme('mytheme_example', myvar);
return $html;
?>
the code i put into the example.tpl.php
<div>
here is the <b><?php print myvar; ?></b>being created.
</div>
i have cleared the cache,but on the node article's page, there is no any output about hello world.
ps:which files i can use the hook_theme, template.php, module file. are there any files i can use this hook?
It looks like you have declared your hook_theme correctly in template.php so I do not think this is the issue.
I did spot a syntax issue with your node.tpl.php, should it not be:
<?php
$vars = array('myvar' => 'hello, world');
$html = theme('mytheme_example', $vars);
return $html;
?>
Note the associate array, with the 'myvar' (the variable declared in hook_theme), is being passed in as the key.
Another point, it is standard practice to name the template file the same as the hook name, so I would suggest calling the template mytheme-example.tpl.php.
See drupal.org for more information
I don't know if you have solved that issue yet.
I would try to declare my theme this way:
function mytheme_theme(){
return array(
'mytheme_example' => array(
'arguments' => array('arguments'=>array()),
'template' => 'example',
),
}
That's how I usually do and it works fine on me.

Resources