Related
After my function.php code was modified I get a white screen when trying to go to my wordpress /wp-admin page. What could be causing the issue? Also general code improvement are welcome, as I never coded in php or wordpress before.
function.php file
<?php
function register_my_menus() {
register_nav_menus(
array(
'header-menu' => __( 'Header Menu' ),
'footer-menu1' => __( 'Footer Menu 1' ),
'footer-menu2' => __( 'Footer Menu 2' ),
'footer-menu3' => __( 'Footer Menu 3' ),
'side-menu' => __( 'Side Menu' ),
)
);
}
add_action( 'init', 'register_my_menus' );
add_theme_support( 'post-thumbnails' );
add_theme_support( 'custom-logo' );
add_filter ( 'nav_menu_css_class', 'so_37823371_menu_item_class', 10, 4 );
function so_37823371_menu_item_class ( $classes, $item, $args, $depth ){
$classes[] = 'nav-item';
return $classes;
}
add_filter( 'nav_menu_link_attributes', function($atts) {
$atts['class'] = "nav-link";
return $atts;
}, 100, 1 );
function my_theme_assets_files() {
wp_enqueue_script( 'custom-js', get_template_directory_uri(). '/js/custom.js' , [], '1.0.0' , true );
wp_localize_script( 'custom-js', 'jsh_ajax', [
'ajax_url' => admin_url( 'admin-ajax.php' )
]);
}
add_action( 'wp_enqueue_scripts', 'my_theme_assets_files' );
add_action("wp_ajax_sk_ajax_posts", "sk_ajax_posts");
add_action("wp_ajax_nopriv_sk_ajax_posts", "sk_ajax_posts");
function sk_ajax_posts() {
$cat_id = $_POST['cat_id'];
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'cat' => $cat_id ,
// 'orderby’ => 'title',
// 'order’ => 'ASC',
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
//the_post();
$categories = get_the_category();
$category_name = $categories[0]->name;
?>
<div class="col-md-4 ">
<div class="card border-0">
<div class="image-parent">
<?php echo wp_get_attachment_image( get_post_thumbnail_id( get_the_ID() ), 'full', '', [ 'class' => 'card-img-top' ] ); ?>
<?php echo $category_name; ?>
</div>
<div class="card-body">
<!-- <h6>April 25, 2022</h6> -->
<h5 class="card-title"><?php the_title(); ?></h5>
<p class="card-text"><?php the_excerpt()?></p>
Read the article
</div>
</div>
</div>
<?php
endwhile;
echo paginate_links(array(
'total' => $loop->max_num_pages,
'prev_text' => __('Previous'),
'next_text' => __('Next')
)) . "</div>";
wp_reset_postdata();
die();
}
/*
* Creating a function to create our CPT
*/
function fisher_custom_post_type_advisor() {
// Set UI labels for Custom Post Type
$labels = array(
'name' => _x( 'Advisor\'s', 'Post Type General Name', 'twentytwentyone' ),
'singular_name' => _x( 'advisor', 'Post Type Singular Name', 'twentytwentyone' ),
'menu_name' => __( 'Advisor\'s', 'twentytwentyone' ),
'parent_item_colon' => __( 'advisor', 'twentytwentyone' ),
'all_items' => __( 'All Advisor\'s', 'twentytwentyone' ),
'view_item' => __( 'View Advisor', 'twentytwentyone' ),
'add_new_item' => __( 'Add New Advisor', 'twentytwentyone' ),
'add_new' => __( 'Add New', 'twentytwentyone' ),
'edit_item' => __( 'Edit Advisor', 'twentytwentyone' ),
'update_item' => __( 'Update Advisor', 'twentytwentyone' ),
'search_items' => __( 'Search Advisor', 'twentytwentyone' ),
'not_found' => __( 'Not Found', 'twentytwentyone' ),
'not_found_in_trash' => __( 'Not found in Trash', 'twentytwentyone' ),
);
// Set other options for Custom Post Type
$args = array(
'label' => __( 'Advisor\'s', 'twentytwentyone' ),
'description' => __( 'Advisor\' news and reviews', 'twentytwentyone' ),
'labels' => $labels,
// Features this CPT supports in Post Editor
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'revisions' ),
// You can associate this CPT with a taxonomy or custom taxonomy.
// 'taxonomies' => array( 'genres' ),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'rewrite' => array( 'slug' => 'advisors' ),
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'show_in_rest' => false,
);
// Registering your Custom Post Type
register_post_type( 'all-advisors', $args );
}
/* Hook into the 'init' action so that the function
* Containing our post type registration is not
* unnecessarily executed.
*/
add_action( 'init', 'fisher_custom_post_type_advisor', 0 );
function global_notice_meta_box() {
add_meta_box(
'advisor-first-name',
__( 'Advisor Detail', 'sitepoint' ),
'global_notice_meta_box_callback',
'all-advisors'
);
}
add_action( 'add_meta_boxes', 'global_notice_meta_box' );
function global_notice_meta_box_callback( $post ) {
// Add a nonce field so we can check for it later.
wp_nonce_field( 'fisher_advisor', 'fisher_advisor' );
global $fields;
$fields = [
array (
'key' => 'first_name',
'label'=> __( 'Full Name'),
'type' => 'text'
),
array (
'key' => 'role',
'label'=> __( 'Title'),
'type' => 'text'
),
array (
'key' => 'professional_certificate_short_form',
'label'=> __( 'Professional Certifications (Short)'),
'type' => 'text'
),
array (
'key' => 'professional_certificate',
'label'=> __( 'Professional Certifications'),
'type' => 'text'
),
array (
'key' => 'adress',
'label'=> __( 'Full Address'),
'type' => 'text'
),
array (
'key' => 'city',
'label' => __( 'Location'),
'type' => 'text'
),
array (
'key' => 'phone',
'label' => __( 'Phone' ),
'type' => 'tel'
),
array (
'key' => 'email',
'label' => __( 'Email' ),
'type' => 'email'
)
];
foreach ( $fields as $field ) {
$value = get_post_meta( $post->ID, '_advisor_'. $field['key'], true );
if ( $field['key'] == 'city' ) {
$args = [ 'post_type' => 'all-locations', 'posts_per_page' => -1 ];
$locations = new WP_Query($args);
if ( $locations->have_posts() ) {
echo '<label><span>' . $field['label'] .'</span>';
echo '<select name="advisor_' . $field['key'] . '">';
while ( $locations->have_posts() ) {
$locations->the_post();
echo '<option value="' . get_the_title() . '">' . get_the_title() . '</option>';
}
echo '</select></label>';
}
} else {
$value = get_post_meta( $post->ID, '_advisor_'. $field['key'], true );
echo '<label><span>' . $field['label'] .'</span> <input type="'. $field['type'] .'" name="advisor_' . $field['key'] . '" value="' . esc_attr( $value ) . '" /></label>';
echo '<br>';
}
}
}
function save_global_notice_meta_box_data( $post_id ) {
// Check if our nonce is set.
if ( ! isset( $_POST['fisher_advisor'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['fisher_advisor'], 'fisher_advisor' ) ) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'all-advisors' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
}
else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
/* OK, it's safe for us to save the data now. */
// Make sure that it is set.
$fields = [
array (
'key' => 'first_name',
'label'=> __( 'Full Name'),
'type' => 'text'
),
array (
'key' => 'role',
'label'=> __( 'Title'),
'type' => 'text'
),
array (
'key' => 'professional_certificate_short_form',
'label'=> __( 'Professional Certifications (Short)'),
'type' => 'text'
),
array (
'key' => 'professional_certificate',
'label'=> __( 'Professional Certifications'),
'type' => 'text'
),
array (
'key' => 'adress',
'label'=> __( 'Full Address'),
'type' => 'text'
),
array (
'key' => 'city',
'label' => __( 'Location'),
'type' => 'text'
),
array (
'key' => 'phone',
'label' => __( 'Phone' ),
'type' => 'tel'
),
array (
'key' => 'email',
'label' => __( 'Email' ),
'type' => 'email'
)
];
foreach ( $fields as $field ) {
if ( ! isset( $_POST['advisor_' . $field['key']] ) ) return;
$sav_field = sanitize_text_field( $_POST['advisor_' . $field['key']] );
update_post_meta( $post_id, '_advisor_' . $field['key'], $sav_field );
}
}
add_action( 'save_post', 'save_global_notice_meta_box_data' );
/*****cpt for location ***/
/*
* Creating a function to create our CPT
*/
function fisher_custom_post_type_location() {
// Set UI labels for Custom Post Type
$labels = array(
'name' => _x( 'Location\'s', 'Post Type General Name', 'twentytwentyone' ),
'singular_name' => _x( 'Location', 'Post Type Singular Name', 'twentytwentyone' ),
'menu_name' => __( 'Location\'s', 'twentytwentyone' ),
'parent_item_colon' => __( 'Location', 'twentytwentyone' ),
'all_items' => __( 'All Location\'s', 'twentytwentyone' ),
'view_item' => __( 'View Location', 'twentytwentyone' ),
'add_new_item' => __( 'Add New Location', 'twentytwentyone' ),
'add_new' => __( 'Add New', 'twentytwentyone' ),
'edit_item' => __( 'Edit Location', 'twentytwentyone' ),
'update_item' => __( 'Update Location', 'twentytwentyone' ),
'search_items' => __( 'Search Location', 'twentytwentyone' ),
'not_found' => __( 'Not Found', 'twentytwentyone' ),
'not_found_in_trash' => __( 'Not found in Trash', 'twentytwentyone' ),
);
// Set other options for Custom Post Type
$args = array(
'label' => __( 'Location\'s', 'twentytwentyone' ),
'description' => __( 'Location\' news and reviews', 'twentytwentyone' ),
'labels' => $labels,
// Features this CPT supports in Post Editor
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'revisions' ),
// You can associate this CPT with a taxonomy or custom taxonomy.
// 'taxonomies' => array( 'genres' ),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'rewrite' => array( 'slug' => 'locations' ),
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'show_in_rest' => false,
);
// Registering your Custom Post Type
register_post_type( 'all-locations', $args );
}
/* Hook into the 'init' action so that the function
* Containing our post type registration is not
* unnecessarily executed.
*/
add_action( 'init', 'fisher_custom_post_type_location', 0 );
/****locatin-meta-box**** */
function global_notice_meta_box_location() {
add_meta_box(
'advisor-first-name',
__( 'Location Detail', 'sitepoint' ),
'global_notice_meta_box_location_callback',
'all-locations'
);
}
add_action( 'add_meta_boxes', 'global_notice_meta_box_location' );
function global_notice_meta_box_location_callback( $post ) {
// Add a nonce field so we can check for it later.
wp_nonce_field( 'fisher_location', 'fisher_location' );
global $fields;
$fields = [
array (
'key' => 'city',
'label'=> __( 'city'),
'type' => 'text'
),
array (
'key' => 'adress',
'label'=> __( 'adress'),
'type' => 'text'
),
array (
'key' => 'phone',
'label' => __( 'Phone' ),
'type' => 'tel'
),
array (
'key' => 'lati',
'label' => __( 'latitutde' ),
'type' => 'text'
),
array (
'key' => 'langi',
'label' => __( 'langitude' ),
'type' => 'text'
),
];
foreach ( $fields as $field ) {
$value = get_post_meta( $post->ID, '_location_'. $field['key'], true );
echo '<label><span>' . $field['label'] .'</span> <input type="'. $field['type'] .'" name="location_' . $field['key'] . '" value="' . esc_attr( $value ) . '" /></label>';
echo '<br>';
}
}
function save_global_notice_meta_box_location_data( $post_id ) {
// Check if our nonce is set.
if ( ! isset( $_POST['fisher_location'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['fisher_location'], 'fisher_location' ) ) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'all-locations' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
}
else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
/* OK, it's safe for us to save the data now. */
// Make sure that it is set.
$fields = [
array (
'key' => 'city',
'label'=> __( 'city'),
'type' => 'text'
),
array (
'key' => 'adress',
'label'=> __( 'adress'),
'type' => 'text'
),
array (
'key' => 'phone',
'label' => __( 'Phone' ),
'type' => 'tel'
),
array (
'key' => 'lati',
'label' => __( 'latitutde' ),
'type' => 'text'
),
array (
'key' => 'langi',
'label' => __( 'langitude' ),
'type' => 'text'
),
];
foreach ( $fields as $field ) {
if ( ! isset( $_POST['location_' . $field['key']] ) ) return;
$sav_field = sanitize_text_field( $_POST['location_' . $field['key']] );
update_post_meta( $post_id, '_location_' . $field['key'], $sav_field );
}
}
When the below was added, I started getting a blank screen for wordpress when I go to /wp-admin
add_action( 'save_post', 'save_global_notice_meta_box_location_data' );
?>
<?php
//SETTING UP THE PAGE
function set_page() {
add_menu_page('Social Media Links', 'Social Media Links', 'manage_options', 'theme_settings', 'the_page', 'dashicons-share', 40);
}
//CALLING THE FUNCTION
add_action('admin_menu', 'set_page');?>
<?php //THE PAGE
function the_page() {?>
<link rel="stylesheet" href="<?php echo get_template_directory_uri()?>/css/bootstrap.min.css">
<legend style="background: #2271b1;color: #fff;padding: 15px 30px;margin-bottom: 60px;margin-top: 10px;padding-right: 85px;max-width: 99%;">Social media linked accounts</legend>
<div class="container"><div class="row"><div class="col-md-12">
<form method="post" action="options.php">
<?php settings_fields('theme_settings'); ?>
<?php $options = get_option('theme_settings'); ?>
<?php if ($_REQUEST['settings-updated']) : ?>
<div class="updated">
<p><strong class="alret">
<?php _e('Options saved'); ?>
</strong></p>
</div>
<?php endif; ?>
<div class="the-container">
<div id="social" class="container tab-pane fade active show"><br>
<fieldset>
<?php $social_arr = ['facebook','twitter','linkedin','youtube'];
foreach($social_arr as $social){?>
<div class="form-group form_builder_row facebook_container row">
<div class="col-md-2 form_builder_col">
<label class="control-label <?php echo $social;?>" for="<?php echo $social;?>">
<?php echo ucfirst($social);?>:</label>
</div>
<div class="col-md-6 form_builder_col">
<input type="text" class=" form_builder_field form-control input-text field_<?php echo $social;?>" id="<?php echo $social;?>" name="theme_settings[<?php echo $social;?>]" value="<?php echo $options[$social];?>" placeholder="<?php echo ucfirst($social);?>">
</div></div>
<?php }?>
</fieldset>
</div>
</div>
<p class="mt-4">
<input name="theme_settings[submit]" id="submit" value="Save Changes" type="submit" class="button button-primary btn-md ml-3">
</p>
</form>
</div></div></div>
<?php } ?>
<?php
//REGISTERING SETTINGS AND FIELDS
function register_settings_and_fields() {
register_setting('theme_settings','theme_settings');
}
add_action('admin_init', 'register_settings_and_fields');
?>
Have you opened wp-config.php? and set WP_DEBUG to true; this can show some errors more intuitively
Don't use function names like this the_page set_page use a prefix before function name e.g.: lefty_wp_set_page , lefty_wp_the_page or give a function a meaningful name, don't just copy paste things from internet, do some modifications in it to avoid conflicts.
I have created a custom plugin on Wordpress that creates a custom post on installation.
The code I use is as follows:
function custom_post_type() {
$labels = array(
'name' => _x( 'Books', 'Post Type General Name', 'my-custom-domain' ),
'singular_name' => _x( 'Book', 'Post Type Singular Name', 'my-custom-domain' ),
'menu_name' => __( 'Book', 'my-custom-domain' ),
'parent_item_colon' => __( 'Parent Book', 'my-custom-domain' ),
'all_items' => __( 'All Books', 'my-custom-domain' ),
'view_item' => __( 'Show Book', 'my-custom-domain' )
);
$args = array(
'label' => __( 'Books', 'my-custom-domain' ),
'description' => __( 'Books and news', 'my-custom-domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
'taxonomies' => array( 'genres' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'show_in_rest' => true,
);
register_post_type( 'libri', $args );
}
add_action( 'init', 'custom_post_type', 0 );
What I would like to do is to be able to create a custom field (again from code) to be inserted under this function and add it to the post created above.
A custom field such as a text field with a title and a label box in which to insert text, or a field consisting of two text boxes.
How can I create it in code and add it to the post so that when I do "Add new" I get this field to fill in?
I mean something like THIS.
There's some really great tutorials that cover this. My answer is taken from this one written by Aileen Javier from WPMU Dev
It's a 3 step process to create custom metaboxes on a page. The first step is to create the metabox using the add_meta_box() function
function libri_add_meta_box( $post ){
add_meta_box( 'libri_meta_box', 'Book Attributes', 'libri_build_meta_box', 'libri', 'side', 'low' );
}
add_action( 'add_meta_boxes_libri', 'libri_add_meta_box' );
The next step is to create a function which we use as the callback parameter in our add_meta_box (in this case 'libri_build_meta_box') - We use this to create inputs for our custom fields. In this instance a text field for Book Author.
function libri_build_meta_box( $post ){
wp_nonce_field( basename( __FILE__ ), 'libri_meta_box_nonce' );
$book_author = get_post_meta( $post->ID, '_book_author', true); ?>
<div class='inside'>
<h3>Book Author</h3>
<p>
<input type="text" name="book_author" value="<?php echo $book_author; ?>" />
</p>
</div>
<?php }
And the final step is to hook into the save_post_{$post->post_type} action, to take the request and then update the post_meta when the post is saved
function libri_save_meta_box_data( $post_id ){
if ( !isset( $_POST['food_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['food_meta_box_nonce'], basename( __FILE__ ) ) ){
return;
}
if ( isset( $_REQUEST['book_author'] ) ) {
update_post_meta( $post_id, '_book_author', sanitize_text_field( $_POST['book_author'] ) );
}
}
add_action( 'save_post_libri', 'libri_save_meta_box_data', 10, 2 );
This will create a metabox on the side of the page that has a single text input for a book author. I'd really recommend reading the whole tutorial from wpmudev, because it's really informative and helpful.
Here's the full code from my example:
<?php function libri_add_meta_box( $post ){
add_meta_box( 'libri_meta_box', 'Book Attributes', 'libri_build_meta_box', 'libri', 'side', 'low' );
}
add_action( 'add_meta_boxes_libri', 'libri_add_meta_box' );
function libri_build_meta_box( $post ){
wp_nonce_field( basename( __FILE__ ), 'libri_meta_box_nonce' );
$book_author = get_post_meta( $post->ID, '_book_author', true); ?>
<div class='inside'>
<h3>Book Author</h3>
<p>
<input type="text" name="book_author" value="<?php echo $book_author; ?>" />
</p>
</div>
<?php }
function libri_save_meta_box_data( $post_id ){
if ( !isset( $_POST['food_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['food_meta_box_nonce'], basename( __FILE__ ) ) ){
return;
}
if ( isset( $_REQUEST['book_author'] ) ) {
update_post_meta( $post_id, '_book_author', sanitize_text_field( $_POST['book_author'] ) );
}
}
add_action( 'save_post_libri', 'libri_save_meta_box_data', 10, 2 );
I've created a CPT front-end form for post-adding. It has a title, description, categories, image, and a couple of custom fields connected with ACF in the backend.
Everything is working, except categories. Once the form is submitted, categories are not added in the single backend post.
Here is the code part where the categories are looped on the front-end:
$args = array(
'taxonomy' => 'types',
'hide_empty' => false,
'hierarchical' => 1
);
wp_dropdown_categories( $args );
And here is the process:
if ( isset( $_POST['post_form'] ) ) {
global $current_user;
wp_get_current_user();
$user_login = $current_user->user_login;
$user_id = $current_user->ID;
$author_name = get_the_author_meta( 'ID', $current_user->ID );
$post_title = sanitize_text_field( $_POST['pas_title'] );
$post_category = (int) $_POST['cat'];
$post_content = sanitize_text_field( $_POST['pas_content'] );
$post_phone = sanitize_text_field( $_POST['pas_phone'] );
$post_location = sanitize_text_field( $_POST['pas_location'] );
$post_price = sanitize_text_field( $_POST['pas_price'] );
$post_condition = sanitize_text_field( $_POST['pas_condition'] );
$new_post = array(
'post_title' => $post_title,
'post_content' => $post_content,
'post_status' => 'publish',
'post_name' => str_replace( ' ', '-', $post_title ),
'post_type' => 'ads',
'post_author' => $author_name,
);
$post_id = wp_insert_post( $new_post );
wp_set_object_terms( $post_id, $post_category, 'types' );
add_post_meta( $post_id, 'meta_key', true );
add_post_meta( $post_id, 'cena', $post_price );
add_post_meta( $post_id, 'lokacija', $post_location );
add_post_meta( $post_id, 'kontakt_telefon', $post_phone );
add_post_meta( $post_id, 'stanje_robe', $post_condition );
add_post_meta( $post_id, 'pas_user_id', $user_id );
if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) {
require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
require_once( ABSPATH . "wp-admin" . '/includes/file.php' );
require_once( ABSPATH . "wp-admin" . '/includes/media.php' );
}
$file_logo = media_handle_upload( 'pas_image', $post_id );
update_post_meta( $post_id, '_thumbnail_id', $file_logo );
}
I've done it before in the same way where everything worked.
If needed, here is the CPT part:
class PAS_Cpt {
public function __construct() {
add_action( 'init', array( $this, 'pas_cpt_ads' ), 0 );
add_action( 'init', array( $this, 'pas_cpt_ads_taxonomy' ), 0 );
}
/**
* CPT Oglasi
*
* #return void
*/
public function pas_cpt_ads() {
$labels = array(
'name' => __( 'Oglasi' ),
'singular_name' => __( 'Oglas' ),
'menu_name' => __( 'Oglasi' ),
'all_items' => __( 'Svi oglasi' ),
'view_item' => __( 'Pogledaj oglas' ),
'add_new_item' => __( 'Dodaj novi Oglas' ),
'add_new' => __( 'Dodaj novi' ),
'edit_item' => __( 'Edituj oglas' ),
'update_item' => __( 'Ažuriraj oglas' ),
'search_items' => __( 'Pretraži oglas' ),
'not_found' => __( 'Nije pronađen' ),
'not_found_in_trash' => __( 'Nije pronađen u kanti' )
);
$args = array(
'label' => __( 'ads' ),
'labels' => $labels,
'supports' => array(
'title',
'editor',
'excerpt',
'author',
'thumbnail',
'revisions',
'custom-fields'
),
'public' => true,
'hierarchical' => false,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'has_archive' => true,
'can_export' => true,
'exclude_from_search' => false,
'yarpp_support' => true,
'taxonomies' => array(
'post_tag'
),
'publicly_queryable' => true,
'capability_type' => 'post',
'menu_icon' => __( 'dashicons-megaphone' )
);
register_post_type( 'ads', $args );
}
/**
* CPT Oglasi Taxonomy
*
* #return void
*/
public function pas_cpt_ads_taxonomy() {
$labels = array(
'name' => _x( 'Kategorije', 'ads' ),
'singular_name' => _x( 'Kategorija', 'ads' ),
'search_items' => __( 'Pretraži kategorije' ),
'all_items' => __( 'Sve kategorije' ),
'parent_item' => __( 'Parent kategorija' ),
'parent_item_colon' => __( 'Parent kategorija:' ),
'edit_item' => __( 'Edituj kategoriju' ),
'update_item' => __( 'Ažuriraj kategoriju' ),
'add_new_item' => __( 'Dodaj novu kategoriju' ),
'menu_name' => __( 'Kategorije' ),
);
register_taxonomy( 'types', array( 'ads' ), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array(
'slug' => 'type'
),
) );
}
}
new PAS_Cpt();
I've been struggling with this for two days. Any help is appreciated.
Thanks
I am trying to delete data from another table using before_delete_post. However, my script does not work. My before_delete_post is at the top of the code. The effect I would like to get when deleting a post would be to delete the data from another table(wp_used_cars) with the same id.
add_action( 'before_delete_post', 'my_func' );
function my_func( $postid ){
global $post_type;
if ( $post_type != 'my_custom_post_type' ) return;
$post_type->query( $wpdb->prepare( 'DELETE FROM wp_used_cars WHERE post_id = %d', $postid ) );
}
// -------------------------------------------------------------------------
// add custom posts --------------------------------------------------------
// add uzywane custom post and set custom capabilities
$uzywane = new CustomPost('uzywane');
$uzywane->make('Uzywane', 'Uzywane', 'Uzywane', array(
'capabilities' => array(
'edit_post' => 'edit_used_car',
'read_post' => 'read_used_car',
'delete_post' => 'delete_used_car',
'edit_posts' => 'edit_used_cars',
'edit_others_posts' => 'edit_others_used_cars',
'publish_posts' => 'publish_used_cars',
'read_private_posts' => 'read_private_used_cars',
),
));
// add this custom capabilities to admin
$role = get_role( 'administrator' );
$role->add_cap( 'edit_used_car', true );
$role->add_cap( 'read_used_car', true );
$role->add_cap( 'delete_used_car', true );
$role->add_cap( 'edit_used_cars', true );
$role->add_cap( 'edit_others_used_cars', true );
$role->add_cap( 'publish_used_cars', true );
$role->add_cap( 'read_private_used_cars', true );
// if role uzywane don't exists add it and set capabilities to edit uzywane
if( ! role_exists( 'uzywane' ) ) {
add_role(
'uzywane',
__( 'Używane' ),
array(
'read' => true,
'edit_used_car' => true,
'read_used_car' => true,
'delete_used_car' => true,
'edit_used_cars' => true,
'edit_others_used_cars' => true,
'publish_used_cars' => true,
'read_private_used_cars'=> true,
'unfiltered_upload' => true,
'upload_files' => true,
'edit_posts' => true,
)
);
}
// if user has role uzywane than remove links from admin panel
$user = wp_get_current_user();
if ( in_array( 'uzywane', (array) $user->roles ) ) {
function remove_links() {
echo '<style>.menu-icon-post, .menu-icon-promo_posts, .menu-icon-serwis, .menu-icon-nowe, .menu-icon-silniki, .menu-icon-comments, #toplevel_page_wpcf7, .menu-icon-tools, #wp-admin-bar-new-content, #wp-admin-bar-wpseo-menu, #wp-admin-bar-comments, .update-nag {display: none;}</style>';
}
add_action('admin_head', 'remove_links');
}
// if role uzywane don't exists add it and set capabilities to edit uzywane
if( ! role_exists( 'uzywane-admin' ) ) {
add_role(
'uzywane-admin',
__( 'Używane-admin' ),
array(
'read' => true,
'edit_used_car' => true,
'read_used_car' => true,
'delete_used_car' => true,
'edit_used_cars' => true,
'edit_others_used_cars' => true,
'publish_used_cars' => true,
'read_private_used_cars'=> true,
'unfiltered_upload' => true,
'upload_files' => true,
'edit_posts' => true,
'edit_others_posts' => true,
'manage_options' => true,
)
);
}
// if user has role uzywane than remove links from admin panel
$user = wp_get_current_user();
if ( in_array( 'uzywane-admin', (array) $user->roles ) ) {
function remove_links() {
echo '<style> #toplevel_page_revslider, #toplevel_page_revslider, #menu-settings, #toplevel_page_wpseo_dashboard, #toplevel_page_edit-post_type-search-filter-widget, #toplevel_page_easy-responsive-tabs, .menu-icon-post, .menu-icon-promo_posts, .menu-icon-serwis, .menu-icon-nowe, .menu-icon-silniki, .menu-icon-comments, #toplevel_page_wpcf7, .menu-icon-tools, #wp-admin-bar-new-content, #wp-admin-bar-wpseo-menu, #wp-admin-bar-comments, .update-nag, #toplevel_page_edit-post_type-acf-field-group .wp-submenu li, #menu-appearance, .acf-columns-2 .wp-list-table.widefat tr, #acf-field-group-locations, #mymetabox_revslider_0, #acf-field-group-options {display: none;} .wp-list-table.widefat tr#post-456 {display: block };</style>';
}
add_action('admin_head', 'remove_links');
}
function cat_for_used_cars() {
$labels = array(
'name' => _x( 'Kategorie samochodów', 'Taxonomy General Name', 'text_domain' ),
'singular_name' => _x( 'Kategoria samochodów', 'Taxonomy Singular Name', 'text_domain' ),
'menu_name' => __( 'Kategoria samochodów', 'text_domain' ),
'all_items' => __( 'Wszystkie samochodów', 'text_domain' ),
'parent_item' => __( 'Parent Item', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'new_item_name' => __( 'Nazwa', 'text_domain' ),
'add_new_item' => __( 'Dodaj', 'text_domain' ),
'edit_item' => __( 'Edycja', 'text_domain' ),
'update_item' => __( 'Update Item', 'text_domain' ),
'view_item' => __( 'View Item', 'text_domain' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ),
'add_or_remove_items' => __( 'Add or remove items', 'text_domain' ),
'choose_from_most_used' => __( 'Choose from the most used', 'text_domain' ),
'popular_items' => __( 'Popular Items', 'text_domain' ),
'search_items' => __( 'Search Items', 'text_domain' ),
'not_found' => __( 'Not Found', 'text_domain' ),
'no_terms' => __( 'No items', 'text_domain' ),
'items_list' => __( 'Items list', 'text_domain' ),
'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_admin_column' => true,
'show_in_nav_menus' => false,
'show_tagcloud' => false,
'rewrite' => false,
);
register_taxonomy( 'usedcars', array( 'uzywane' ), $args );
}
add_action( 'init', 'cat_for_used_cars', 0 );
Try Below code :
add_action( 'before_delete_post', 'my_func' );
function my_func( $postid )
{
global $post_type;
if ( $post_type != 'uzywane' )
return;
global $wpdb;
$prefix = $wpdb->prefix;
$tbl = $prefix.'used_cars';
$used_cars_count = $wpdb->get_var( "SELECT COUNT(*) FROM $tbl where post_id = ". $postid );
if($used_cars_count > 0)
{
$wpdb->delete( $tbl, array( 'post_id' => $postid ) );
}
}
Hope this will help you!
When we set permalink as Post name and go to wordpress any default post Like "Testing 123" single page its link looks like this
localhost/foo_articles/testing-123
Now i when we change our permalink to Custom Structure and set value like %category%/%postname%, the link looks like this
http://localhost/foo_articles/testing/testing-123/
testing is my category slug
Now the main part of my question is
I make a plugin where i create a post type foo_articles and custom taxonomy foo_categories
Its work perfectly. When i click on a category its link looks like this
http://localhost/foo_articles/foo_category/junk-food/
and when i click on an article for a single page, its link looks like this
http://localhost/foo_articles/foo_articles/how-to-reduce-the-intake-of-junk-food-in-children/
foo_articles is my post type and its a change able
Now my question is how can i set links that when a user set permalinks Custom Structure and set value like %category%/%postname% my link also change like above default post single page.
http://localhost/foo_articles/article cat slug/how-to-reduce-the-intake-of-junk-food-in-children/
Here is custom post type code:
add_action('init', 'foo_articles');
function foo_articles() {
$foo_slug = 'foo_articles';
$foo_slug = get_option('foo_plugin_slug');
$labels = array(
'name' => __('Foo', 'fff'),
'singular_name' => __('Foo', 'fff'),
'all_items' => __('Articles', 'fff'),
'add_new' => __('New Article', 'fff'),
'add_new_item' => __('Add New Article', 'fff'),
'edit_item' => __('Edit Article', 'fff'),
'new_item' => __('New Article', 'fff'),
'view_item' => __('View Articles', 'fff'),
'search_items' => __('Search Articles', 'fff'),
'not_found' => __('Nothing found', 'fff'),
'not_found_in_trash' => __('Nothing found in Trash', 'fff'),
'parent_item_colon' => ''
);
$foo_rewrite = array(
'slug' => FOO_PLUGIN_SLUG, // i define this in plugin index file
'with_front' => true,
'pages' => false,
'feeds' => true,
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'menu_icon' => plugin directory.'images/icon-foo.png',
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 3,
'supports' => array('title','editor','thumbnail','comments','tags'),
'rewrite' => $foo_rewrite,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => true
);
register_post_type( 'foo_articles' , $args );
flush_rewrite_rules();
}
add_action( 'init', 'foo_taxonomies', 0 );
// Article taxonamy
function foo_taxonomies() {
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => __( 'Article Category', 'fff'),
'singular_name' => __( 'Article Category', 'fff' ),
'search_items' => __( 'Search Article Category', 'fff' ),
'all_items' => __( 'All Article Categories', 'fff' ),
'parent_item' => __( 'Parent Article Category', 'fff' ),
'parent_item_colon' => __( 'Parent Article Category:', 'fff' ),
'edit_item' => __( 'Edit Article Category', 'fff' ),
'update_item' => __( 'Update Article Category', 'fff' ),
'add_new_item' => __( 'Add New Article Category', 'fff' ),
'new_item_name' => __( 'New Article Category Name', 'fff' ),
'menu_name' => __( 'Categories', 'fff' )
);
register_taxonomy( 'foo_categories', array( 'foo_articles' ), array(
'hierarchical' => true,
"labels" => $labels,
"singular_label" => __( 'Foo Category', 'foo'),
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'foo_category', 'with_front' => true )
));
flush_rewrite_rules();
}
Note: i change my post type slug by plugin settings and its option_name is foo_plugin_slug (its a client idea)
So please tell me how can i do this. Is there any hook or filter or htaccess code
You can use WP Walker concept. Please check this
WP Walker
Eg:
Use ACF plugin to get custom field.
List pages code :-
$args = array(
'child_of' => $post->ID,
'date_format' => get_option('date_format'),
'post_type' => 'page',
'title_li' => __(''),
'walker' => new my_page_walker
);
wp_list_pages( $args );
Extend Walker_page in function.php
In function.php
class my_page_walker extends Walker_Page {
public function start_el( &$output, $page, $depth = 0, $args = array(), $current_page = 0 ) {
if ( $depth ) {
$indent = str_repeat( "\t", $depth );
} else {
$indent = '';
}
$css_class = array( 'page_item', 'page-item-' . $page->ID );
if ( isset( $args['pages_with_children'][ $page->ID ] ) ) {
$css_class[] = 'page_item_has_children';
}
if ( ! empty( $current_page ) ) {
$_current_page = get_post( $current_page );
if ( $_current_page && in_array( $page->ID, $_current_page->ancestors ) ) {
$css_class[] = 'current_page_ancestor';
}
if ( $page->ID == $current_page ) {
$css_class[] = 'current_page_item';
} elseif ( $_current_page && $page->ID == $_current_page->post_parent ) {
$css_class[] = 'current_page_parent';
}
} elseif ( $page->ID == get_option('page_for_posts') ) {
$css_class[] = 'current_page_parent';
}
$css_classes = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );
if ( '' === $page->post_title ) {
$page->post_title = sprintf( __( '#%d (no title)' ), $page->ID );
}
$args['link_before'] = empty( $args['link_before'] ) ? '' : $args['link_before'];
$args['link_after'] = empty( $args['link_after'] ) ? '' : $args['link_after'];
$page_permalink = get_permalink( $page->ID );
$user_defined_link = get_field('my-custom-field',$page->ID)['url'];
if (!is_null($user_defined_link)) {
$page_permalink = $user_defined_link;
}
$output .= $indent . sprintf(
'<li class="%s">%s%s%s',
$css_classes,
$page_permalink,
$args['link_before'],
apply_filters( 'the_title', $page->post_title, $page->ID ),
$args['link_after']
);
if ( ! empty( $args['show_date'] ) ) {
if ( 'modified' == $args['show_date'] ) {
$time = $page->post_modified;
} else {
$time = $page->post_date;
}
$date_format = empty( $args['date_format'] ) ? '' : $args['date_format'];
$output .= " " . mysql2date( $date_format, $time );
}
}
}
The final link will be the value from the custom field.