Cannot redirect to Discourse. Error: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource

If it was my site, I’d just do something like this, to add a login button to the WordPress menu that will automatically login and create users.

add_filter( 'wp_nav_menu_items', 'discourse_community_link', 10, 2 );
function discourse_community_link( $items, $args ) {
	if ( 'primary' === $args->theme_location ) {
		if ( is_user_logged_in() ) {
			$community_link = '<li><a href="http://localhost:3000/session/sso?return_path=/">Community</a> </li>';
		} else {
			$community_link = '<li><a href="http://localhost:3000">Community</a> </li>';
		}
		$items = $items . $community_link;
	}

	return $items;
}

It can also be done by embedding a Discourse iframe on the page that users are redirected to after logging in. Look at part C of this guide for how to do it: https://meta.discourse.org/t/wordpress-integration-guide/27531?u=simon_cossar I think you need to use an embed tag instead of an iframe to get it to work.

Edit: the code I added above is assuming your menu location is called ‘primary’. It could be called something else. You can check the value of the $args parameter to see what it is.

3 Likes