具有自定义帖子类型的WordPress post_row_action不起作用

时间:2018-09-11 08:06:45

标签: php wordpress

我正在尝试使用post_row_actions删除用户在用户仪表板上垃圾分类的事件类型“事件”的能力,但是脚本不起作用。

网址是wp-admin / edit.php?post_type = event

add_filter( 'post_row_actions', 'remove_row_actions', 10, 1 );
function remove_row_actions( $actions )
{
if( get_post_type() === 'event' )
    unset( $actions['edit'] );
    unset( $actions['view'] );
    unset( $actions['trash'] );
    unset( $actions['inline hide-if-no-js'] );
return $actions;

我也尝试过将post_row_action与page_row_action交换不成功

我知道脚本通过在if语句中使用简单的回显进行测试来正确检测帖子类型。它只是不会删除仪表板上的任何链接

2 个答案:

答案 0 :(得分:0)

尝试此代码,

public class CameraFragment extends Fragment implements OnMapReadyCallback{
 double latitude, longitude;
 Button myplacebtn;
MapView mapView;
 GoogleMap map;

 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.camera_fragment, container, false);
         myplacebtn = (Button) view.findViewById(R.id.myplacebtn);//Button --get my location

         mapView = (MapView) view.findViewById(R.id.mapView);
        mapView.onCreate(savedInstanceState);
        //get a MapView
        mapView.getMapAsync(new OnMapReadyCallback() {

            @Override
            public void onMapReady(GoogleMap googleMap) {
                map = googleMap;
                // Setting a click event handler for the map
                map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {

                    @Override
                    public void onMapClick(LatLng latLng) {

                        // Creating a marker
                        MarkerOptions markerOptions = new MarkerOptions();

                        // Setting the position for the marker
                        markerOptions.position(latLng);
                        setCoordinates.setText(latLng.latitude + " , " + latLng.longitude);
                        latitude = latLng.latitude;
                        longitude = latLng.longitude;

                        // Setting the title for the marker.
                        // This will be displayed on taping the marker
                        markerOptions.title(latLng.latitude + " : " + latLng.longitude);

                        // Clears the previously touched position
                        map.clear();

                        // Animating to the touched position
                        map.animateCamera(CameraUpdateFactory.newLatLng(latLng));

                        // Placing a marker on the touched position
                        map.addMarker(markerOptions);
                    }
                });
                map.getUiSettings().setMyLocationButtonEnabled(false);


                LatLng jerusalem = new LatLng(32.1105435, 34.8683683);
                CameraUpdate miLocation = CameraUpdateFactory.newLatLngZoom(jerusalem, 11);
                map.moveCamera(CameraUpdateFactory.newLatLng(jerusalem));
                googleMap.animateCamera(miLocation);
                MarkerOptions markerOptions = new MarkerOptions();
                markerOptions.position(jerusalem);

                if (ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(),
                        android.Manifest.permission.ACCESS_FINE_LOCATION)
                        != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(),
                        android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        requestPermissions(new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION,
                                android.Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.INTERNET}, MY_REQUEST_INT);
                    }
                    return;
                } else {
                    googleMap.setMyLocationEnabled(true);
                }

                googleMap.getUiSettings().setZoomControlsEnabled(true);

            }
        });



        }



    @Override
    public void onResume() {
        mapView.onResume();
        super.onResume();
    }


    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
    }

}

您可能还需要以下内容:

/**
 * Removes the "Trash" link on the individual post's "actions" row on the posts
 * edit page.
 */
add_filter( 'post_row_actions', 'remove_row_actions_post', 10, 2 );
function remove_row_actions_post( $actions, $post ) {
    if( $post->post_type === 'prj' ) {
        unset( $actions['clone'] );
        unset( $actions['trash'] );
    }
    return $actions;
}

add_action('wp_trash_post', 'restrict_post_deletion');
function restrict_post_deletion($post_id) {
    if( get_post_type($post_id) === 'prj' ) {
      wp_die('The post you were trying to delete is protected.');
    }
}

/**
 * Removes the "Delete" link on the individual term's "actions" row on the terms
 * edit page.
 */
add_filter( 'tag_row_actions', 'remove_row_actions_term', 10, 2 );
function remove_row_actions_term( $actions, $term ) {
    if ( 'org' === $term->taxonomy ) {
        unset( $actions['delete'] );
    }
    return $actions;
}

add_action( 'pre_delete_term', 'restrict_taxonomy_deletion', 10, 2 );
function restrict_taxonomy_deletion( $term, $taxonomy ) {
    if ( 'org' === $taxonomy ) {
        wp_die( 'The taxonomy you were trying to delete is protected.' );
    }
}

add_action( 'admin_head', function () {
    $current_screen = get_current_screen();

    // Hides the "Move to Trash" link on the post edit page.
    if ( 'post' === $current_screen->base &&
    'prj' === $current_screen->post_type ) :
    ?>
        <style>#delete-action { display: none; }</style>
    <?php
    endif;

    // Hides the "Delete" link on the term edit page.
    if ( 'term' === $current_screen->base &&
    'org' === $current_screen->taxonomy ) :
    ?>
        <style>#delete-link { display: none; }</style>
    <?php
    endif;
} );

尝试此代码。希望!!它的工作。

答案 1 :(得分:0)

您可以删除用户的删除功能。尝试下面的代码

add_action( 'admin_init', 'remove_user_delete_capability' );

function remove_user_delete_capability() {      
   global $wp_roles;
   // Remove capabilty from editor role users 
   $wp_roles->remove_cap('editor', 'delete_posts');
   $wp_roles->remove_cap('editor', 'delete_others_posts');
   $wp_roles->remove_cap('editor', 'delete_published_posts');
}

在以上代码中,我仅编辑者角色具有删除功能。您可以根据需要进行操作。

要从仪表板上删除链接,请使用以下代码

add_filter( 'views_edit-post', function( $views )
{

  $remove_views = [ 'trash' ];

  foreach( (array) $remove_views as $view )
  {
      if( isset( $views[$view] ) )
        unset( $views[$view] );
  }
  return $views;
} );