custom image search page in wordpress - php

Please Help
I want to make a search page for images stored in media gallery of wordpress. This page contains a textbox in which user will enter the number of image. The matched image should display in the different tab of browser.
I made a raw html page in wordpress where i put the which display a textbox but don't understand that where i have to write the php code to extract the images and how to show them.
Please give step by step instructions to solve the issue because i am beginner in wordpress.

You can create a custom function in functions.php for this and you can call that in your template
//In functions.php
if( ! ( function_exists( 'get_media_from_name' ) ) ) {
function get_media_from_name( $postName ) {
$args = array(
'name' => $postName,
'posts_per_page' => -1,
'post_type' => 'attachment',
);
$attachment = new WP_Query( $args );
if ( $attachment)
return $attachment;
else
return false;
}
}
//In you template where you can call that function
$get_all_matched_attachments = get_media_from_name( $postName );
if ( $get_all_matched_attachments ) {
foreach($get_all_matched_attachments as $get_all_matched_attachment){
echo '<pre>';print_r($get_all_matched_attachment);
}
}

Related

Hide wordpress page completely

I develop my first plugin, which creates some pages programatically by this method:
$page_id = wp_insert_post(
array(
'comment_status' => 'close',
'ping_status' => 'close',
'post_author' => 1,
'post_title' => 'HSN Form',
'post_name' => strtolower(str_replace(' ', '-', trim('hsn-form'))),
'post_status' => 'publish',
'post_type' => 'page',
)
);
And I set template file for it:
add_filter( 'page_template', 'hsn_service_form_page_template', 10, 1 );
function hsn_service_form_page_template( $page_template ){
if ( is_page( 'hsn-form' ) ) {
$page_template = plugin_dir_path(__DIR__) . 'service-form/form-template.php';
}
return $page_template;
}
After I would like to hide it completely from wordpress dashboard, but those have to available following way:
wwww.example.com/hsn-form.
I can hide it following code from Pages menu:
add_filter( 'parse_query', 'ts_hide_pages_in_wp_admin' );
function ts_hide_pages_in_wp_admin($query) {
global $pagenow,$post_type;
$page = get_page_by_path('hsn-form');
if (is_admin() && $pagenow=='edit.php' && $post_type =='page') {
$query->query_vars['post__not_in'] = array($page->ID);
}
}
It's ok, but it's still available in Appereance->Menu, where you can create nav menus.
I searched for it, but I can't find complete solution for my problem:
My plugin has to create some pages which are available this way: www.example.com/hsn-form
I need to hide this pages completely from admin dashboard
I would like to apply templates for these pages to add some custom php code.
So If somebody should know a complete solution for it, that should be great.
Thank you in advance!
change parse_query to pre_get_posts
In order to remove a page from the admin nav menu, you can hook into the admin_menu action, then manipulate the global $submenu variable.
add_action('admin_menu', function(){
global $submenu;
array_walk($submenu, function(&$child, $parent){
if( $parent != 'edit.php' )
return;
foreach($child as $key=>$submenu){
if( $submenu[2] == 'post-new.php' ) {
unset($child[$key]);
break;
}
}
});
});
In that example, we are looking for a subpage with the slug of post-new.php
underneath the top level page with the slug edit.php. If found, it gets altogether removed from the navigation menu.
The $submenu[2] part is looking for the slug element in the array. If you want to match by the submenu name instead, you can replace it with $submenu[0].

Editing the page does not work if I use get_template_part in shortcode

Help solve the problem, please!
I have template part content-event.php. I use it in the loop of custom post type "event" in archive-event.php It works well here.
Now I need to make a carousel of these posts (events). My code in functions.php:
add_shortcode('km_events_carousel', 'km_events_carousel');
function km_events_carousel(){
$html = '<div class="your-class">';
$args = array(
'post_type'=> 'km_event',
'meta_key' => 'start',
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
$km_events_query = new WP_Query( $args );
if( $km_events_query->have_posts() ) {
while ( $km_events_query->have_posts() ) {
ob_start();
get_template_part( kettlebell_get_post_template_part_slug(), 'event' );
$html .= ob_get_clean();
}
}
$html .= '</div>';
wp_reset_postdata();
return $html;
}
This shortcode also works well.
The essence of the problem: when I click on the "Edit page" on the top of the page or open the page from the admin panel for editing, I get a picture of one of the posts that appear in the carousel instead of the admin panel.
If I comment function get_template_part() - all works correctly. But of course, then I do not get posts in the carousel.
Tell me, please, what could be the error or some workaround.

Wordpress functions

So I have had a little search and maybe I am searching for the wrong thing. I am trying to run a function from my functions.php file in wordpress and assign the returned array to a variable.
When I run the function, it just echos out the returned data rather than assigning it to the variable.
I'm assuming this is a Wordpress thing.
Code from functions.php
function get_portfolioBlock( ) {
$portfolio_query = new WP_Query(array('post_type' => 'portfolio', 'posts_per_page' => 1, 'name' => 'test1'));
if ( $portfolio_query->have_posts() ) {
while ($portfolio_query->have_posts() ) {
$portfolio_query->the_post();
$portfolio_block = array('title' => the_title(), 'excerpt' => the_excerpt() );
}
return $portfolio_block;
} else {
return 'error';
}
}
The code in my template file
<?php $portfolio = get_portfolioBlock(); ?>
When the page loads it automatically loads the data into the page without assigning the variable for me to use else where
Thank you Gerald!
The reason for it echoing in out the function was because when I was assigning the_title and the_excerpt I should have been using
get_the_title() and get_the_excerpt()

WORDPRESS: Insert SWF in home page instead of post

I have wordpress flash game website. Into the admin panel, where I add or edit post I have field named Swf URL: where I add SWF link and it links to the post where it shows flash game. I want to do same but not in the post, I want to use this function (adding game with the help of swf) to home page.
How it is now:
How I want to see:
functions code:
load_theme_textdomain( "freebabyhazelgames", TEMPLATEPATH."/languages" );
$locale = get_locale( );
$locale_file = TEMPLATEPATH."/languages/{$locale}.php";
if ( is_readable( $locale_file ) )
{
require_once( $locale_file );
}
$sp_boxes = array( "Game Details" => array( array( "thumb", "Game image URL:" ), array( "game", "Swf URL:" ) ) );
add_action( "admin_menu", "sp_add_custom_box" );
add_action( "save_post", "sp_save_postdata", 1, 2 );
if ( !function_exists( "get_custom_field" ) )
{
function get_custom_field( $field )
{
global $post;
$custom_field = get_post_meta( $post->ID, $field, true );
echo $custom_field;
}
}
post code:
<div id="playgame">
<script type="text/javascript">
<!--
swf("<?php $values = get_post_custom_values("game"); echo $values[0]; ?>", "701", "550");//-->
</script>
</div>
By the way I tried to change dirrectory or to copy single.php to index.php but both not worked.
If I understand you right, you want the game to appear on the main page without entering a post? In that case, I think you can try using a static frontpage instead of latest posts.
You can find it on Appearance -> Customize, and then choose the frontpage to be static. That way you can have your game on the static frontpage.

Redirect Wordpress User to their own Post on Front-End Login

I'm trying to make the front-end login for Wordpress redirect to the post (in a custom post type) that was automatically created when they registered.
I can get the URL I want to redirect them with a wp_query. I imagine this is not the best way to do it, but I don't know enough php to figure it out. Here's my current attempt, but it just prints the url (the right one, at least!) on a blank page with the same login url they were already at:
function my_login_redirect( $redirect_to, $request, $user ){
global $user, $post;
$args = array(
'author' => $current_user->ID,
'post_type' => 'course-providers',
'showposts' => 1,
'caller_get_posts' => 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php wp_redirect ( the_permalink () ); ?>
<?php
endwhile;
} else {
echo "This User Has no Profile";
}
}
add_filter("login_redirect", "my_login_redirect", 10, 3);
Also, I imagine I don't need the wp_redirect and that I should just be using the login_redirect filter itself, but again, I'm pretty lost right now and just taking lots of shots in the dark.
Thanks for the help, and let me know if there's additional info that would make this more helpful for others or easier to answer. Thanks!
I ended up using a template redirect to make this work. I assume there's technically a better way to do it, but it's loading extremely fast and doing exactly what I need it to.
So, now, when a user logs in it goes to a direct url- /profiles - and the template on that page is just a redirect. I used the ideas and some sample code from this smashing magazine post on random redirects to make it work.
Here's the function I used in my functions.php file for the template to make the redirect happen:
function profile_redirect() {
// This is a template redirect
// Whenever someone goes to /profile (or any page using the profile template)
// this function gets run
if ( is_user_logged_in() ) {
global $current_user, $post;
$args = array(
'author' => $current_user->ID,
'post_type' => 'profile',
'posts_per_page' => 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ( $my_query->have_posts() )
$my_query->the_post();
//We have a post! Send them to their profile post.
wp_redirect ( get_permalink () );
exit;
} else {
// If there are no posts, send them to the homepage
wp_redirect ( get_bloginfo('url') );
exit;
}
wp_reset_query();
} else {
// If they're not logged in, send them to the homepage
wp_redirect ( get_bloginfo('url') );
exit;
}
}
Then, on my profile template, I put this at the top with the opening php tag to run the function:
profile_redirect(); ?>
This is working for me, so I'll leave it as is for now :)

Categories